Forum Moderators: phranque

Message Too Old, No Replies

Redirect for nonexistent domain

         

folkbloke

12:10 pm on Mar 13, 2006 (gmt 0)

10+ Year Member



On an old server on of the virtual sites was used to trap any domains that pointed to that IP, but didn't have a virtual site.
Each domain went to a seperate page.

So for example www.fred.co.uk
ended up at www.another.co.uk/www_fred.htm

This was done using a .htaccess file in site1 (where unrecognised domains end up), as follows:

RewriteEngine On
# Check it's not the main domain ( example.com )
RewriteCond %{HTTP_HOST}!^(www\.)?example\.com$ [NC]
# Extract the domain name without the www. part
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.co.uk$ [NC]
# Test to avoid internal loops
RewriteCond %{QUERY_STRING}!^dom=.+$ [NC]
# Goto test domain
RewriteRule .* [another.co.uk...] [R=permanent,L]

I know that I had to do something else to get it working, but can't remember what. It's also possible that the above wasn't the final version.

Any ideas. Please.

jdMorgan

8:33 pm on Mar 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The 'test to avoid loop' code looks obsolete, in that the rule does not use a dom= query string any more.

I'd suggest:


RewriteEngine on
# Check it's not the main domain ( example.com )
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com [NC]
# Test to avoid internal loops
RewriteCond %{HTTP_HOST} !^www\.another\.co\.uk [NC]
# Extract the domain name without the www part
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.co\.uk [NC]
# Goto test domain
RewriteRule .* http://www.another.co.uk/www_%2.htm [R=permanent,L]

Don't anchor domain names, unless you add regex to catch appended port numbers, e.g.

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.co\.uk(:[0-9]{1,5})?$ [NC]

Jim

folkbloke

10:26 am on Mar 15, 2006 (gmt 0)

10+ Year Member



Thanks for that. It does tidy up my code but it still doesn't work.

Having done some tests it seems that by the time the .htaccess file in the default domain (site1) is accessed, the {HTTP_HOST} has already been re-written to that domain name. So the requested domain doesn't seem to be available.

That doesn't sound right. Any ideas.

jdMorgan

12:43 am on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest discussing your findings with your Web host. If the client-requested domain name is being rewritten in httpd.conf, then thers' no way to 'recover' it in .htaccess.

Jim