In order to remove index.html
or index.htm
from urls I use the following in my .htaccess
RewriteCond %{REQUEST_URI} /i
To avoid double redirection have another rule in .htaccess file that meets both conditions like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule . http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . %1 [R=301,NE,L]
So if input URL is http://mydomain.com/path/index.html
then both the conditions get satisfied in the first rule here and there will be 1 single redirect (301) to http://www.mydomain.com/path/
.
Also I believe QSA
flag is not really needed above since you are NOT manipulating query string.
A better solution would be to place the index.html rule ahead of the www rule and inside the index.html rule ADD the www prefix to the destination url. This way someone looking for http://domain.com/index.html would get sent to http://www.domain.com/ by the FIRST rule. The second (www) rule would then only apply if index AND www are missing, which is again only one redirect.
Remove the L
flag from the prior rule? L forces the rule parsing to stop (when the rule is matched) and thus send the first rewritten URL without applying the second rule.
The rules are applied sequentially from top to bottom, each rewriting the URL again if it matches the rule's conditions and pattern.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteRule ^(.*/)index\.html?$ $1 [NC,QSA,R=301,NE,L]
Hence the above will first add the www
and then remove the index.html?
, before sending the new URL; A single redirect for all the rules.