重写规则以隐藏文件夹,如果没有尾部斜杠则无法正常工作
我有一个奇怪的 apache mod_rewrite 问题。我需要对用户隐藏一个子目录,但将每个请求重定向到该子目录。我在 stackoverflow 上发现了几个非常相似的问题,但没有什么真正适合的,所以我决定发布一个新问题。
我的 .htaccess 看起来像这样:
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)?$ foo/$1 [QSA,L]
文档根目录仅包含以下文件夹/文件:
/foo/bar/index.html
我现在希望 example.com/bar 和 example.com/bar/ 只会向我显示 index.html 的内容。
相反,example.com/bar/ 按预期显示内容,但 example.com/bar 使用 301 将我重定向到 example.com/bar/foo/,然后显示内容。我真的不明白为什么在这种情况下会有 301 重定向。
当我将这个东西放在
RewriteCond %{REQUEST_URI} !^[^.]*/$ RewriteCond %{REQUEST_URI} !^[^.]*\.html$ RewriteCond %{REQUEST_URI} !^[^.]*\.php$ RewriteRule ^(.*)$ $1/ [QSA,L]
该规则之上时,它似乎可以工作,但这需要我列出每个使用的文件扩展名...
有没有其他方法可以省略重定向,文件夹“bar”永远不应该被看到外部用户。
提前致谢!
i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.
My .htaccess looks like this:
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)?$ foo/$1 [QSA,L]
The document-root only contains the following folder/files:
/foo/bar/index.html
I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.
Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.
When i put something this
RewriteCond %{REQUEST_URI} !^[^.]*/$ RewriteCond %{REQUEST_URI} !^[^.]*\.html$ RewriteCond %{REQUEST_URI} !^[^.]*\.php$ RewriteRule ^(.*)$ $1/ [QSA,L]
on top of that rule it seems to work, but that would require me to list every used file extension...
Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
迟到总比不到好...
让它与一个简单的 RewriteRule 一起工作,该规则将 / 附加到每个没有的 url 上。
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
第一个重写规则是从 /foo/(.) 重定向到 ($1),第二个重写规则是从 (.) 重定向到 $1。
只是想法,尚未经过测试。
1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.
just idea, this has not been tested.