Forum Moderators: phranque

Message Too Old, No Replies

Using .htaccess to host multiple domains one space

How do I use .htaccess to host more than one domain on a single hosting acc

         

codebru

2:21 am on Mar 27, 2009 (gmt 0)

10+ Year Member



I've read the post at htaccess with regards to hosting 2 domains on one web space [webmasterworld.com]...

I'd like to be able to have:
example1.com
example2.com
example3.com.au
All on the one space which doesn't allow subdomains, etc. I can have aliases which are already set up.

Following jdMorgan's tips from the linked thread above, how would I code the .htaccess for the three domains, each of which will reside in the following folders:
ex1
ex2
ex3

TIA

jdMorgan

1:42 pm on Mar 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your best-effort code as a starting point for discussion. See also the Apache "URL Rewriting Guide" which contains example code for implementing massive numbers of virtual servers using mod_rewrite.

I should point out that it's usually better to use the standard VirtualHost definition method in httpd.conf or other server-config files instead of using mod_rewrite. Doing so has many advantages related to security and site and server administration. I can't recommend using the mod_rewrite approach unless you simply have no other choice.

Jim

codebru

11:25 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



The code that seems best to me is:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteCond $1!^ex1/
RewriteRule (.*) /ex1/$1 [L]
#
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com
RewriteCond $1!^ex2/
RewriteRule (.*) /ex2/$1 [L]
#
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.com\.au
RewriteCond $1!^ex3/
RewriteRule (.*) /ex3/$1 [L]

I don't think I have access to httpd.conf and would now have to wait until Monday to find out for sure in any case.

jdMorgan

1:06 am on Mar 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to add spaces between "$1" and "!" in your RewriteConds, but otherwise, that would work.

However, you could save some code and eliminate maintenance by using the domain name (or the domain name plus TLD) prefixed with a few 'tag' characters (for loop-stopping) as the subdirectory name:


Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.com [OR]
RewriteCond %{HTTP_HOST}>example1 ^(>)(.+)$
RewriteCond $1 !h_
RewriteRule ^(.*)$ /h_%2/$1 [L]

Note the 'trick' in the second RewriteCond used to rewrite blank Host requests to the h_example1 subdirectory.
Both the ">" character in that RewriteCond and the "h_" prefix on host subdirectories are arbitrary; The only requirement is that they be unambiguously identifiable by pattern-matching.

I'd suggest that you add a 301-redirect rule above this one to force canonicalization of all domain names to either "www" or "non-www" both for SEO/duplicate-content-prevention reasons, and so that you don't have to allow for the "www" being optional in all subsequent rules. For example, if you canonicalize to "www" the preceding code simplifies to:


Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com [OR]
RewriteCond %{HTTP_HOST}>example1 ^>(.+)$
RewriteCond $1 !h_
RewriteRule ^(.*)$ /h_%1/$1 [L]

Jim

codebru

5:01 am on Mar 28, 2009 (gmt 0)

10+ Year Member



Hi jdMorgan,

I've used example1.com, example2.com, etc in this example because in previous threads I have read you admonishing people for using actual domains and told them to use 'example.com'

My understanding of your sample code would be that it literally works for three domains with a numeral at the end. How would it work for:
bob.com
jane.org
spot.com.au
?

I have further questions re your sample code, but they are dependant on your reply to this question.

Thanks,

Bruce

jdMorgan

5:08 am on Mar 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can wait for replies, or you can test it yourself... I suggest the latter.

The code has no dependency on the domain names, except that they all end with .com -- as currently shown, but easy enough to change...

Jim