Welcome to WebmasterWorld Guest from 54.159.246.164

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

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

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

5+ Year Member



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?

thanks

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

WebmasterWorld Senior Member 10+ Year Member



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]

HTH

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

5+ Year Member



That did it! Thanks so much Wheelie!
3:13 pm on Mar 30, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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)

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




# 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]

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



That's the one!
12:09 am on Apr 3, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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)

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



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.

Jim