重写规则以隐藏文件夹,如果没有尾部斜杠就无法正常工作 [英] Rewrite rule to hide folder, doesn't work right without trailing slash
问题描述
我有一个奇怪的 apache mod_rewrite 问题.我需要对用户隐藏一个子目录,但将每个请求重定向到该子目录.我在 stackoverflow 上发现了几个非常相似的问题,但没有一个真正适合,所以我决定发布一个新问题.
我的 .htaccess 如下所示:
<前>重写引擎开启重写基数/RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-l重写规则 ^(.*)?$ 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$重写规则 ^(.*)$ $1/[QSA,L]在该规则之上它似乎有效,但这需要我列出每个使用过的文件扩展名...
有没有其他方法可以省略重定向,文件夹bar"不应该被外部用户看到.
提前致谢!
迟到总比不到好...
让它使用一个简单的 RewriteRule,它在每个没有打开的 url 后附加一个/.
# 仅目录RewriteCond %{REQUEST_FILENAME} !-f# 排除有目录RewriteCond %{REQUEST_URI} !^/excluded-dirs# 排除这些扩展RewriteCond %{REQUEST_URI} !\.excluded-extension$# 排除已经有/的请求RewriteCond %{REQUEST_URI} !(.*)/$重写规则 ^(.*)$/$1/[R=301,L]
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!
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !\.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]
这篇关于重写规则以隐藏文件夹,如果没有尾部斜杠就无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!