Welcome to WebmasterWorld Guest from 54.147.0.174

Forum Moderators: brotherhood of lan & mack

Message Too Old, No Replies

non-www redirected in .htaccess Rewrite?

     

ctmtb

5:36 pm on Jan 6, 2009 (gmt 0)

5+ Year Member



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]

jdMorgan

4:45 am on Jan 7, 2009 (gmt 0)

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



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.

Jim

ctmtb

8:26 pm on Jan 7, 2009 (gmt 0)

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month