Redirect Question

m0rtal

King of the Jungle
Jul 3, 2007
3,007
93
0
Toronto
I just realized I was using this to redirect non-www to www.

Code:
RewriteCond %{HTTP_HOST} !^www\.url\.com$ [NC]
RewriteRule ^(.*)$ http://www.url.com/$1 [R=301,L]

It works, but shouldn't this be the proper way?

Code:
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Reason I ask is a newer domain I'm running still shows both www and non www in Google and it uses the 1st format. It does redirect properly so I dunno, what the heck is the difference if any? I know Goog takes a while to get it right but just sayin.
 


It depends on what the value of HTTP_HOST is.

On the first one, if HTTP_HOST is subdomain.example.com it would rewrite it to www .example.com (without the space after www)

The second one would never apply because subdomain.example.com would not satisfy the rewrite condition, only the string "example.com" would.