Question

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>
Was it helpful?

Solution

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>

OTHER TIPS

you can try replacing your first rewriterule with following

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

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top