.htaccess 将根目录重定向到子文件夹

发布于 2024-08-22 23:23:24 字数 463 浏览 2 评论 0原文

我希望 mod_rewrite 将所有请求重定向到不存在的文件和文件夹,并将所有对主文件夹(“根”)的请求重定向到子文件夹。所以我是这样设置的:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC,OR]
RewriteCond %{REQUEST_URI} / [NC]
RewriteRule ^(.*)$ /my/subfolder/$1 [L,QSA]

不幸的是,它不起作用:如果我请求 example.com/public/ 它会重定向到我的处理脚本(因此重定向到 my/subfolder/index.php?app=public )文件夹“public”存在。 请注意,请求domain.com/正确地重定向到my/subfolder/index.php

这是为什么?

I would like mod_rewrite to redirect all requests to non existing files and folders, and all requests to the main folder ("root") to a subfolder. So i've set it up like this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC,OR]
RewriteCond %{REQUEST_URI} / [NC]
RewriteRule ^(.*)$ /my/subfolder/$1 [L,QSA]

Unfortunately, it does not work: if i request example.com/public/ it redirects to my processing script (so redirecting to my/subfolder/index.php?app=public ) although the folder "public" exists.
Note that requesting domain.com/ correctly redirects to my/subfolder/index.php

Why is that?

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

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

发布评论

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

评论(1

把梦留给海 2024-08-29 23:23:24

它不起作用,因为你的最后一个条件不仅匹配根,还匹配任何包含 / 的 uri,这基本上就是一切。请尝试以下操作:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /my/subfolder/$1 [L,QSA]

请注意,不需要 [NC],因为您不尝试匹配任何字母,因此实际上并不需要“无大小写”。

希望有帮助。

it does not work because your last condition is not matching only the root but any uri that has / in it, which is basically everything. Try the following instead:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /my/subfolder/$1 [L,QSA]

Note that [NC] is not needed as you are not trying to match any alphabets, so "No Case" is not really needed.

Hope it helps.

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