homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

301 redirect for non-www AND index.html to www

 2:06 am on Mar 30, 2009 (gmt 0)

Hi everybody, trying to write htaccess to send example.com and www.example.com/index.html to www.example.com. So far I have:

AddType application/x-httpd-php .html
AddType text/x-server-parsed-html .htm .html

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

but this only does the example.com and not the www.example.com/index.html

Can someone help me with some code please please?




 10:48 am on Mar 30, 2009 (gmt 0)

Hi ponchaz and welcome to the forum

Try this after what you already have

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



 11:24 am on Mar 30, 2009 (gmt 0)

That did it! Thanks so much Wheelie!


 3:13 pm on Mar 30, 2009 (gmt 0)

Wait. There are multiple problems with that.

First. That code only works for the single index file located in the site root. You should fix this for all folders at any level. There's example code posted in many prior threads.

Second. If you place this redirect after your non-www to www redirect then you create an unwanted redirection chain for a non-www request that contains the index filename.

To fix this, use the example code posted in earlier threads, and place that code before the general non-www to www redirect.

Be aware that your non-www to www redirect code can be improved in several subtle ways too. Several prior threads discuss the reasons.


 5:37 pm on Mar 30, 2009 (gmt 0)

# Externally redirect requests for index.html in any directory to "/" in that directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
# Externally redirect requests for *all* non-canonical hostnames to canonical hostname,
# including case errors and appended FQDN indicator and/or port numbers.
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]



 6:37 pm on Mar 30, 2009 (gmt 0)

That's the one!


 12:09 am on Apr 3, 2009 (gmt 0)

That's the one!

However, I usually replace the \.html part with something like \.(s?html?php[45]?[aj]spx?cfm) to make it more universal.


 1:35 am on Apr 3, 2009 (gmt 0)

A balance would likely be best: I'd recommend redirecting only index.xyz URLs that actually exist on your site, or that have existed on your site in the past and still have inbound links.


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