To be able to remove index.html or index.htm from web addresses I personally use the next during my .htaccess

RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]

This works! (More information about flags in the finish of the question *)

Then to be able to add www in web addresses I personally use the next during my .htaccess

RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ "http://www.mydomain.com/$1" [R=301,NE,L]

This works too!

The question here's how to prevent the double redirection produced by rules above in the event such as the one below:

  1. browsers requests http://mydomain.com/path/index.html
  2. server transmits 301 header to redircet browser to http://mydomain.com/path/
  3. then browser demands http://mydomain.com/path/
  4. the server transmits 301 header to redircet browser to http://www.mydomain.com/path/

This really is clearly not so wise result in a poor user who's asking http://mydomain.com/path/index.html could be double rerouted, and that he would feel page goes not fast enough. Furthermore Googlebot might stop following a link induce to the double redircetion (I am unsure about this 4g iphone and I'd rather not enter into attorney at law about this, it is simply another possible problem.)

Thanks!


*To whom it may be interested:

  • NC can be used to redirect also uppercased files i.e. INDEX.HTML / InDeX.HtM
  • NE can be used to prevent double url encoding I avoid http://.../index.html?hello=ba%20be to become rerouted to http://.../index.html?hello=ba%2520be
  • QSA can be used to redirect also queries, i.e. http://.../index.html?hello=babe to http://.../?hello=babe (unnecessary because of anubhava answer)

To prevent double redirection have another rule inshtaccess file that fits both conditions such as 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]

Therefore if input URL is http://mydomain.com/path/index.html then both conditions get satisfied within the first rule in some places is going to be 1 single redirect (301) to http://www.mydomain.com/path/.

Also In my opinion QSA flag isn't needed above as you are NOT adjusting query string.

Take away the L flag in the prior rule? L forces the rule parsing to prevent (once the rule is matched up) and therefore send the very first rewritten URL without using the 2nd rule.

The guidelines are applied sequentially all the way through, each spinning the URL again whether 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 mentioned will first add the www after which take away the index.html?, before delivering the brand new URL Just one redirect for all the guidelines.