2

我最近将 index.html 更改为 index.php - 我希望能够进行重定向以反映这一点,然后还进行重写以强制 foo.com/index.php 成为 foo.com/ 并使其访问.php 文件。

我还有一个单独的站点,即 bar.com,它位于 foo.com 下的子目录中,我希望不受影响。

这就是我所拥有的,这会导致重定向循环:

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]

有没有办法做到这一点?谢谢!

4

1 回答 1

5

RedirectMatch指令在接受请求并将其重定向到的内部重定向之后匹配,然后它再次通过 URL 文件映射管道,从而匹配您的重定向指令。//index.html

您可以尝试包括:

DirectoryIndex index.php

以防止这种自动内部重定向。您还应该像使用文件一样使用%{THE_REQUEST}match :index.htmlindex.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]

或者你可以完全绕过 index.php:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
于 2013-05-02T23:48:33.990 回答