Forum Moderators: phranque

Message Too Old, No Replies

Domain Mapping Using .htaccess

mapping multiple domains on same account

         

Moff

4:27 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



Hi,

I know this can be done in http.conf but is it possible to use .htaccess & Mod_rewrite to map domain names to a directory so that you can run multiple independant sites from the same hosting account?

Is the following code along the right lines?


RewriteCond %{HTTP_HOST} ^www\.secondary-site\.co\.uk [NC]
RewriteRule (.*) /websites/secondarysite/$1 [L]

jdMorgan

12:36 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but you may need to prevent a looping problem:

RewriteCond %{HTTP_HOST} ^(www\.)?secondary-site\.co\.uk [NC]
RewriteCond %{REQUEST_URI} !^/websites/secondary-site/
RewriteRule (.*) /websites/secondary-site/$1 [L]

The other tweak is to accept www- or non-www.

Jim

Moff

3:09 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



Thanks for that jdmorgan,

I knew I was missing something, not sure if I need the final tweak to accept the www and non-www as I thought I think im forcing the www. version

Full code

RewriteCond %{HTTP_HOST} ^primary-site\.co\.uk [NC]
RewriteRule (.*) [primary-site.co.uk...] [R=301,L]

RewriteCond %{HTTP_HOST} ^secondry-site\.co\.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?additional-url\.co\.uk [NC]
RewriteRule (.*) [secondary-site.co.uk...] [R=301,L]

RewriteCond %{HTTP_HOST} ^secondary-site\.co\.uk [NC]
RewriteCond %{REQUEST_URI}!^/websites/
RewriteRule (.*) /websites/$1 [L]

Does this setup look alright?

jdMorgan

7:36 pm on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but I think you're missing the "www\." in the last RewriteCond. Since "www." will be added by the second rule, the third rule won't ever match as it stands now.

Jim

Moff

8:50 pm on Mar 2, 2005 (gmt 0)

10+ Year Member



Just realised this still doesn't work quite properly but can't figure out why.

If I try going to

[primary-site.co.uk...] it works perfectly

However, if I go to [primary-site.co.uk...]

It presents a 302 redirect to [secondary-site.co.uk...]

Can anyone point me in the right direction to find out why? Any help greatly appreciated!

Present Code:

RewriteCond %{HTTP_HOST} ^primary-site\.co\.uk [NC]
RewriteRule (.*) [primary-site.co.uk...] [R=301,L]

RewriteCond %{HTTP_HOST} ^secondry-site\.co\.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?additional-url\.co\.uk [NC]
RewriteRule (.*) [secondary-site.co.uk...] [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.secondary-site\.co\.uk [NC]
RewriteCond %{REQUEST_URI}!^/websites/
RewriteRule (.*) /websites/$1 [L]

jdMorgan

9:18 pm on Mar 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



None of your code does a 302 redirect. So look elsewhere for the code that's doing this. It could be something your host is doing, a domain-forwarding "service", etc.

Jim