仅当文件夹存在但文件不存在时重写

发布于 2024-11-19 04:31:34 字数 573 浏览 5 评论 0原文

我试图将所有请求重写到一个不存在但文件夹存在的文件。

我的意思是:

假设我有这个文件夹结构:

foo
  '-bar1
  '-bar2
    '-bar2.html
  '-shared
     '-shared.html

我正在寻找的重写规则要做的是正常提供 example.com/foo/bar2/bar2.html 。将 example.com/foo/bar1/bar1.html 提供为 /foo/shared/shared.html,并且不提供 example.com/foo/bar3/bar3.html。

综上所述。我正在尝试开发一个 RewriteCond,仅当目录存在但该目录中不存在该文件时才会命中。

问题是:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

即使 /foo/bar3 目录不存在,它也会重写 www.example.com/bar3/bar3.html 。

感谢您的帮助!

I am trying to rewrite all requests to a file that doesn't exist but the folder does.

What I mean is:

Say I have this folder structure:

foo
  '-bar1
  '-bar2
    '-bar2.html
  '-shared
     '-shared.html

What I am looking for the rewrite rule to do is to serve up example.com/foo/bar2/bar2.html as normal. Serve example.com/foo/bar1/bar1.html as /foo/shared/shared.html and to no serve example.com/foo/bar3/bar3.html.

So in summary. I am trying to develop a RewriteCond that hits only when the directory exists but the file doesnt exist in that directory.

The problem with:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

is that it will rewrite www.example.com/bar3/bar3.html even though /foo/bar3 directory doesn't exist.

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2024-11-26 04:31:34

这些规则应该完成这项工作:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1/ -d
RewriteRule ^(.+)/([^/]+)$ /foo/shared/shared.html [L,QSA]
  1. 这些规则旨在放置在根文件夹中的.htaccess文件中。如果您需要将它们放在配置文件中(例如httpd-vhost.conf),那么您将需要对其进行一些修改(删除第二个RewriteCond中的一些斜杠..或在RewriteRule中添加前导斜杠 -实际上需要测试)。

  2. 仅当请求的资源至少有 1 个文件夹(明显 - 按要求)时,这些规则才有效。

These rules should do the job:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1/ -d
RewriteRule ^(.+)/([^/]+)$ /foo/shared/shared.html [L,QSA]
  1. These rules intended to be placed in .htaccess file in root folder. If you need to place them in config file (e.g. httpd-vhost.conf) then you will need to modify it a bit (remove some slashes in 2nd RewriteCond .. or add leading slash in RewriteRule -- testing is actually required).

  2. These rules will only work if requested resource has at least 1 folder (obvious -- as requested).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文