.htaccess 重写:内部为 /mypage,但在 URL 中看起来像 /Home/mypage

.htaccess 重写:内部为 /mypage,但在 URL 中看起来像 /Home/mypage

我可能完全搞错了:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]
#Rewrites so it looks like production
RewriteCond %{REQUEST_URI} !^/Home/
RewriteRule ^(.*)$ /Home/$1 [L]

有人能帮我把所有网址都弄直吗http://site/somepage1http://site/somepage2,改写为http://site/Home/somepage1http://site/Home/somepage2。是否有一条规则可以捕获所有这些 URL,并在所有 URL 前面添加“/Home”?

提前致谢。

更新

进入 httpd.conf 并设置“LogLevel = debug”后发现两个规则存在冲突:

[Fri Sep 23 10:54:07 2011] [debug] core.c(3065): [client 127.0.0.1] r->uri = /Home/index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /Home/index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /Home/index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /Home/index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /Home/index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /index.php
[Fri Sep 23 10:54:07 2011] [debug] core.c(3071): [client 127.0.0.1] redirected from r->uri = /Home

有没有办法将这两个重写规则结合起来而不会出现这个重定向错误?

答案1

就这么简单:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/Home/
RewriteRule ^(.*)$ /Home/$1 [L]

显然,如果您/Home/直接请求其中的 URL(例如http://site/Home/somepage1,它将不会被重写为/Home/Home/somepage1- 因此如果您的子文件夹与主文件夹同名,请记住这一点。


更新: 考虑到新的信息,让我们尝试这种方法:

Options +FollowSymLinks
RewriteEngine On

# rewrite incoming link to /Home/
# but only if it's a non-existing file
RewriteCond %{REQUEST_URI} !^/(Home/|index\.php)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /Home/$1 [L]

# route all requests for non-existing resources to /index.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]

相关内容