homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Non-WWW to WWW version

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

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)

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)

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)

Yes, this is fine.


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

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)

Thats great, Jim!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

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