Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Non-WWW to WWW version

5:55 am on Jun 30, 2009 (gmt 0)

10+ Year Member

This code is in my htaccess file and it works but I want to be 100% sure that it is correctly coded.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule (.*) [domain.com...] [R=301,L]

6:21 am on Jun 30, 2009 (gmt 0)

5+ Year Member

The code is correct. however, you can also add the code to redirect your index file (www.example.com/index.html) to the www.example.com version:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.example.com/$1 [R=301,L]

7:02 am on Jun 30, 2009 (gmt 0)

10+ Year Member

Sorry I forgot to add, but this is under the code I posted first. Is this the same as what you suggested?

RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ [domain.com...] [R=301,L]

8:38 am on Jun 30, 2009 (gmt 0)

5+ Year Member

Yes, this is fine.
1:04 pm on Jun 30, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The index page redirect should go first --before that hostname canonicalization redirect-- to avoid 'stacked' or 'chained' redirects in the case where "/index.html" is requested from the "wrong" domain.

In general, to avoid problems you should put your external redirect rules in order, from most-specific pattern to least-specific pattern, followed by your internal rewrite rules, again ordered from most-specific to least-specific pattern.

I also suggest the use of the more-robust pattern "^(([^/]+/)*)index\.html$" instead of "^(.*)index.html$" in that index redirect rule, making it:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]

This is more specific, and avoids unexpectedly redirecting --for example-- "newindex.html" or "index.htmlx".


1:13 pm on Jun 30, 2009 (gmt 0)

10+ Year Member

Thats great, Jim!

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved