I recently changed my index.html to index.php - I want to be able to do a redirect to reflect this, and then also a rewrite that forces foo.com/index.php to just be foo.com/ and have it access the .php file.
我最近修改了索引。html索引。php -我希望能够重定向来反映这个,然后重写,让foo。com/index。php成为foo。com/,让它访问。php文件。
I also have a separate site i.e. bar.com located in a subdirectory under foo.com that I want to remain unaffected.
我还有一个单独的网站,bar.com,位于foo.com的子目录中,我希望不受影响。
This is what I had, which results in a redirect loop:
这就是我所拥有的,它导致了一个重定向循环:
RedirectMatch 301 ^/index.html?$ /index.php
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
Is there a way to do this? Thanks!
有办法吗?谢谢!
1 个解决方案
#1
5
The RedirectMatch
directive matches after the internal redirect that takes the /
request and redirects it to /index.html
, then it get put through the URL-file mapping pipeline again and thus matches your redirect directive.
RedirectMatch指令在接受/请求并将其重定向到/index的内部重定向之后匹配。然后,它再次通过url文件映射管道,从而匹配重定向指令。
You can try including a:
你可以尝试包括以下内容:
DirectoryIndex index.php
to prevent this automatic internal redirect. You should also use the %{THE_REQUEST}
match with the index.html
file like you're doing with index.php
:
为了防止这种自动内部重定向。您还应该使用%{THE_REQUEST}匹配索引。html文件,就像你使用index.php一样:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1index.php [R=301,L]
Or you can bypass index.php entirely:
或者你可以绕过索引。php完全:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
#1
5
The RedirectMatch
directive matches after the internal redirect that takes the /
request and redirects it to /index.html
, then it get put through the URL-file mapping pipeline again and thus matches your redirect directive.
RedirectMatch指令在接受/请求并将其重定向到/index的内部重定向之后匹配。然后,它再次通过url文件映射管道,从而匹配重定向指令。
You can try including a:
你可以尝试包括以下内容:
DirectoryIndex index.php
to prevent this automatic internal redirect. You should also use the %{THE_REQUEST}
match with the index.html
file like you're doing with index.php
:
为了防止这种自动内部重定向。您还应该使用%{THE_REQUEST}匹配索引。html文件,就像你使用index.php一样:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1index.php [R=301,L]
Or you can bypass index.php entirely:
或者你可以绕过索引。php完全:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]