Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite problem

         

etwas

12:25 am on Jun 26, 2005 (gmt 0)



I want to rewrite www.something.com to something.com externaly. We have virtual hosts, if that's a problem. I created a .htaccess file in the root folder of my web (where index.html resides):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.something\.com [NC]
RewriteRule ^/(.*) http://something.com/$1 [R,L]

Nothing happens. No error, just nothing.
htaccess (redirect e.g.) is working so no probs with httpd.conf I guess.

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?

jd01

3:30 am on Jun 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi etwas

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