Web Hosting Talk







View Full Version : Odd <host>.domain.com redirection


huck
08-30-2001, 11:49 AM
I want to be able to redirect sites from anything.domain.com to www.domain.com. So, I setup some RewriteRules to do this for me. I made sure to set up the DNS with a wildcard to catch all of the various possibilities.

Now host.domain.com works, aljdljfdljfldjf.domain.com works but get this:
anything.domain.com does not work
What is going on here.
Relevant httpd.conf snippets

ServerAlias *.domain.com

RewriteEngine on
RewriteCond %{HTTP_HOST} !^domain.com(:80)?$
RewriteCond %{HTTP_HOST} !^www.domain.com(:80)?$
RewriteRule ^/(.*) http://www.domain.com/$1 [L,R]



Any clues???? Just curious.

Jm4n
08-30-2001, 01:09 PM
Personally I wouldn't use mod_rewrite -- your ServerAlias directive will make sure they see the same content no matter what URL they use. I don't really see the need to redirect the user. I'm not sure it's worth the overhead of mod_rewrite...

That said, this isn't tested, but try changing the last line to:
RewriteRule ^(/.*)? http://www.domain.com$1 [L,R]
I'm not positive, but I don't think the trailing slash will necessarily always be there (or perhaps a :80 might be before it, in which case you'll need to further modify it...)

Again, not tested, but you might look at your logs and make sure the requested URI would actually match the conditions and the regex.

huck
08-30-2001, 01:44 PM
Nevermind ...
It seems to be working now, which is odd. I am not sure why it did not work before.


As for mod_rewrite, I use it for other purposes as well, which is why it is enabled. Cobalt Raqs use mod_rewrite and mod_perl to dyanamically build up redirection for the site admin panel.

As far as redirecting the user -- this ensures that they end up at www.domain.com as opposed to something else, which provides consistency.

If the trailing slash is not there, then I think the RewriteCond should fail, thus no rewritting.

RewriteCond %{HTTP_HOST} !^domain.com(:80)?$

Jm4n
08-30-2001, 03:19 PM
If the trailing slash is not there, then I think the RewriteCond should fail, thus no rewritting.

Not when you're checking HTTP_HOST -- this wouldn't contain any part of the URI, only what the brower sends in the Host: header... so it should match no matter what URI is passed in the GET request.

What I meant was the RewriteRule's regex -- though now that my brain has decided to cooperate, I realize of course that the browser will always issue a "GET /", thus the slash will always be part of the request URI :)

So, nevermind me...