2

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 i.e. 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!

0

1 Answer 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.

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely beautiful. Exactly what I wanted/needed. Thanks a million!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.