重写规则以隐藏文件夹,如果没有尾部斜杠则无法正常工作

发布于 2024-10-05 09:48:31 字数 879 浏览 1 评论 0原文

我有一个奇怪的 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 技术交流群。

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

发布评论

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

评论(2

伴我老 2024-10-12 09:48:32

迟到总比不到好...

让它与一个简单的 RewriteRule 一起工作,该规则将 / 附加到每个没有的 url 上。

# 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]

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]
痴意少年 2024-10-12 09:48:32

第一个重写规则是从 /foo/(.) 重定向到 ($1),第二个重写规则是从 (.) 重定向到 $1。

只是想法,尚未经过测试。

1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.

just idea, this has not been tested.

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