1

I have to do some redirects - index.html and index.php to /.

I could redirect www.domain.com/index.html and www.domain.com/index.php to www.domain.com with this line:

RewriteRule ^index\.(php|html?)$ http://www.mydomain.com/ [R=301,L]

However I still have problems with pages like these:

  • www.domain.com/directory/index.html - returns 200 OK
  • www.domain.com/directory/index.php - returns 300 multiple choices (don't know why)

Is it possible to redirect them to www.domain.com/directory/ and do it all with one rule one?

If this helps, here's what's in my .htaccess file now:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

## Redirecd index.html & php to /
RewriteRule ^index\.(php|html?)$ http://www...mydomain...com/ [R=301,L]

## 301 Redirect from non-www to www version
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

## Return 404 status code and display 404.html
ErrorDocument 404 /404.html

</IfModule>

3 Answers 3

2

Try this code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

## Redirecd index.html & php to /
RewriteCond %{THE_REQUEST} /index\.(html|php) [NC]
RewriteRule ^(.*?)index\.(?:html|php)$ /$1 [L,R=302,NC,NE]

## 301 Redirect from non-www to www version
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

## Return 404 status code and display 404.html
ErrorDocument 404 /404.html

</IfModule>
Sign up to request clarification or add additional context in comments.

Comments

0

you can try replacing your first rewriterule with following

RewriteRule ^/?(.+/)?index\.(php|html?)$ /$1 [R=301,L]

3 Comments

This will cause looping if index.html or index.php are used in DirectoryIndex
thanks @anubhava for your comment, I didn't (and still havn't) test it with DirectoryIndex set to said files. Is there a way we can avoid such issue?
Yes it can be avoided by using RewriteCond %{THE_REQUEST} as I suggested.
0

Please try this. This works fine with any directory.

RewriteCond %{THE_REQUEST} ^.*/index\.(html|php)

RewriteRule ^(.*)index\.(html|php)$ http://www.domain.com/$1 [R=301,L]

Comments

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.