Forum Moderators: phranque
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.something\.com [NC]
RewriteRule ^/(.*) http://something.com/$1 [R,L]
I've tried many things but without luck. Any help appreciated!
Ps: In the Apache URL rewriting guide example (Canonical Hostnames) there are two lines with negative conditions:
RewriteCond %{HTTP_HOST}!^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R] What are these for?
Welcome to WebmasterWorld
RewriteRule ^/(.*) http://something.com/$1 [R,L]
The problem with this rule is the preceding / is stripped by Apache in the .htaccess file, so, this will not match in a comparison.
RewriteRule (.*) http://something.com/$1 [R=301,L]
Please, also notice the R=301. R or a full canonical domain default to a 302 temporary request. By adding 301 to the R you can define the redirect as permanent. I also removed the ^ to mark the beginning of the line, because when you are storing the entire request it is actually just an extra character.
The two condition negative version is for HTTP 1.0 clients, which do not send HOST headers. In this case the second condition checks to see if the HTTP_HOST is empty and if it is, does not redirect, becuase if it did a client that did not have a host header would create an infinite loop by not ever sending a header that says www.domain.com, even if that was the version they were accessing.
A shorter, (slightly) more efficient version of the negative version (which I prefer because it can actually break some attempts to frame a site) is:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC]
RewriteRule (.*) http://fully.qualified.domain.name/$1 [L,R=301]
Hope this helps.
Justin