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 / WebmasterWorld / New To Web Development
Forum Library, Charter, Moderators: brotherhood of lan & mack

New To Web Development Forum

non-www redirected in .htaccess Rewrite?

5+ Year Member

Msg#: 3820359 posted 5:36 pm on Jan 6, 2009 (gmt 0)

Main site: www.example.com
I have lots of others that seem to be 301'd correctly. But I'm thinking I forgot an important one, the main site.

In Google Webmaster Tools, this is what it tells me for ""In external links to your site"

1. http www example com
2. http example com
3. example com

Should I add to my .htaccess file this?
RewriteCond %{HTTP_HOST} ^example.com$ [OR]

This is my current file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$ [OR]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$ [OR]
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
RewriteRule ^/?(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301,L]

[edited by: eelixduppy at 5:00 am (utc) on Jan. 7, 2009]
[edit reason] exemplified [/edit]



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

Msg#: 3820359 posted 4:45 am on Jan 7, 2009 (gmt 0)

Yes, but three things:

First, you should remove the "$" from the end of all hostname patterns, in case someone requests the FQDN by appending a period, or in case they append a port number. This is a perfectly valid hostname: www.example.com.:80

Second, you can cut the number of lines in half by replacing the pairs of www- and non-www lines with one line making the "www." optional:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [OR]

Do *not* do this with your new RewriteCond for the main domain, though, or you'll cause a redirection loop!
Note also that the literal periods are escaped.

Third, if you have no domains pointed to this server that *should not* be redirected to the canonical domain, in other words, if all non-canonical domains pointed to your server should be redirected, then you can replace that whole pile of RewriteConds with one line:

# Redirect to the canonical domain if request is not for *exactly* the canonical hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Note here the negation operator on the RewriteCond pattern, the end-anchor which *should be* on that negated pattern, the elimination of unnecessary tokens in the RewriteRule pattern, and the removal of the unneeded quotes and escaping in the substitution URL.



5+ Year Member

Msg#: 3820359 posted 8:26 pm on Jan 7, 2009 (gmt 0)

Ahh I didn't see the message that you replied! Thank you for the info that is great.

Global Options:
 top home search open messages active posts  

Home / Forums Index / WebmasterWorld / New To Web Development
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