Currently the home page of my website is accessible via these URL's:

www.example.com, example.com, www.example.com/index.html, and example.com/index.html

So I'm trying to set up redirects so that example.com goes to www.example.com AND that www.example.com/index.html, www.example.com/index.html goes to www.example.com to avoid duplicate content issues. Tried this in the .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]
Redirect 301 /index.html http://www.example.com/

But got error message that there were too many redirects. What do I need to include this .htaccess file to make this 301 redirect? I also have sub-domains like fr.example.com, de.example.com So do I also need to write separate 301 redirect rules/codes in their .htaccess file?

1 Answer 1


Something such as the below will handle the canonical www redirect:-

    RewriteEngine On
## canonical www 301 redirect   
    RewriteCond %{HTTP_HOST} ^example.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

The best rule to rewrite and 301 your various homepage URL's will vary depending on the website platform your website is built on though. Something along the lines of this may work though:-

## rewrite and 301 index pages
    RewriteRule ^index\.(php|html?)$ http://www.example.com/ [R=301,L]
  • Thanks for the quick answer. My website is running on Apache web server. Do I need to include these 2 codes (## canonical www 301 redirect and ## rewrite and 301 index pages) in the htaccess file?
    – Jane
    Nov 26, 2014 at 8:58
  • The # just means they are comments. Comments to help you understand what rules do what in the file. You don't have to put them if you want (and you can change the wording next to the # to read better for you if you prefer). Your website maybe hosted on an Apache server (but we know that anyway as you're using an .htaccess) but your website may be built on a CMS platform such as Wordpress, Magento, Prestashop, Joomla etc where different rules will work for different purposes.
    – zigojacko
    Nov 26, 2014 at 9:12
  • You could try all the suggested .htaccess contents in my answer and see how that works, then tackle anything that still isn't working after (implement in stages so you can test along the way).
    – zigojacko
    Nov 26, 2014 at 9:13
  • It's working, thank you so much. Can I also use this code to redirect my sub-domains?
    – Jane
    Nov 26, 2014 at 9:27
  • Without knowing an actual list of every single required variation it is hard to say - whilst you could list multiple instances of the above, it is often possible to come up with one rule or two to handle the lot using regex - maybe something such as RewriteRule ^(.*/)?index\.html?$ /$1 [R=301,L] would handle all the index.html rewrites for sub domains for example.
    – zigojacko
    Nov 26, 2014 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.