如何htaccess将index.html重定向到index.php和index.php到/

Ben*_*Coy 2 apache .htaccess redirect rewrite

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

我还有一个单独的网站,即位于foo.com下的子目录中的bar.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]
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*Lin 5

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

您可以尝试包括:

DirectoryIndex index.php
Run Code Online (Sandbox Code Playgroud)

防止这种自动内部重定向.您还应该使用%{THE_REQUEST}与index.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]
Run Code Online (Sandbox Code Playgroud)

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)