仅当文件夹存在但文件不存在时重写
我试图将所有请求重写到一个不存在但文件夹存在的文件。
我的意思是:
假设我有这个文件夹结构:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些规则应该完成这项工作:
这些规则旨在放置在根文件夹中的.htaccess文件中。如果您需要将它们放在配置文件中(例如
httpd-vhost.conf
),那么您将需要对其进行一些修改(删除第二个RewriteCond中的一些斜杠..或在RewriteRule中添加前导斜杠 -实际上需要测试)。仅当请求的资源至少有 1 个文件夹(明显 - 按要求)时,这些规则才有效。
These rules should do the job:
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).These rules will only work if requested resource has at least 1 folder (obvious -- as requested).