homepage Welcome to WebmasterWorld Guest from 54.161.191.254
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
ponchaz




msg:3881439
 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?

thanks

 

wheelie34




msg:3881572
 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]

HTH

ponchaz




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

That did it! Thanks so much Wheelie!

g1smd




msg:3881708
 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.

jdMorgan




msg:3881806
 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]

Jim

g1smd




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

That's the one!

g1smd




msg:3884443
 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.

jdMorgan




msg:3884478
 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.

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