Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving 'index.html' from url and adding 'www' with one single 301 redirect
    primarykey
    data
    text
    <p>In order to remove <code>index.html</code> or <code>index.htm</code> from urls I use the following in my <code>.htaccess</code></p> <pre><code>RewriteCond %{REQUEST_URI} /index\.html?$ [NC] RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L] </code></pre> <p><strong>This works!</strong> (More info about flags at the end of this question *)</p> <p>Then in order to add <code>www</code> in urls I use the following in my <code>.htaccess</code></p> <pre><code>RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC] RewriteRule ^(.*)$ "http://www.mydomain.com/$1" [R=301,NE,L] </code></pre> <p><strong>This works too!</strong></p> <p>The question here is <strong>how to avoid the double redirection created by rules above in cases like the one below:</strong></p> <ol> <li><strong>browsers asks for <code>http://mydomain.com/path/index.html</code></strong></li> <li><strong>server sends <code>301</code></strong> header to redircet browser to <code>http://mydomain.com/path/</code></li> <li>then browser requests <code>http://mydomain.com/path/</code></li> <li>now the <strong>server sends <code>301</code></strong> header to redircet browser to <code>http://www.mydomain.com/path/</code></li> </ol> <p>This is obviously not very smart cause a poor user who is asking <code>http://mydomain.com/path/index.html</code> would be double redirected, and he would feel page goes too slow. Moreover Googlebot might stop following the link cause to the double redircetion (I'm not sure on this last one and I don't want to get into a discussion on this, it's just another possible issue.)</p> <p>Thanks!</p> <hr> <p>*To whom it might be interested:</p> <ul> <li><code>NC</code> is used to redirect also uppercased files i.e. <code>INDEX.HTML</code> / <code>InDeX.HtM</code></li> <li><code>NE</code> is used to avoid double url encoding I avoid <code>http://.../index.html?hello=ba%20be</code> to be redirected to <code>http://.../index.html?hello=ba%2520be</code></li> <li><strike><code>QSA</code> is used to redirect also queries, i.e. <code>http://.../index.html?hello=babe</code> to <code>http://.../?hello=babe</code></strike> (not needed thanks to <a href="https://stackoverflow.com/questions/6059920/removing-index-html-from-url-and-adding-www-with-one-single-301-redirect/6062534#6062534">anubhava answer</a>)</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload