简体   繁体   中英

How do I htaccess redirect index.html to index.php and index.php to /

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.

I also have a separate site ie bar.com located in a subdirectory under foo.com that I want to remain unaffected.

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!

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.

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 :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM