Forum Moderators: phranque

Message Too Old, No Replies

htaccess, multi domains and sub domains

htaccess, multi domains and sub domains

         

loveunit

7:19 pm on Mar 17, 2008 (gmt 0)

10+ Year Member



Hi,

I am a regular viewer of WW.com, however this is my first post, as I usually find the answer hidden away here some place, but this one has me stuck...

so, in brief, I am using a shared hosting system - which requires you to set-up a new account for each domain, however I am making a build-your-own website system, which uses shared code and a shared mySQL db, so what I need is to point each domain to a specific folder..

I have this and it works ok, with a few notable issues:

# www.domain-1.com
RewriteCond %{HTTP_HOST} ^www.domain-1.com$
RewriteCond %{REQUEST_URI} !^/folder/1/
RewriteRule ^(.*)$ folder/1/$1 [L]

# www.domain-2.com
RewriteCond %{HTTP_HOST} domain-2.com
RewriteCond %{REQUEST_URI} !^/folder/2/
RewriteRule ^(.*)$ folder/1/$1 [L]

to explain, domain-1.com is also my website, and contains the web builder tool, so is a bit special, it also has a load of sub domains, for example css.domain-1.com, which are an important part of how it all works..

domain-2.com is an example client site, and this works, both for http and www only requests.. so no worries there.

what I need is that requests to http or just www work for domain-1.com without breaking my sub domains, which end up being rewritten is I use the more generic condition used for domain-2.com.

at present www requests work fine, but http only requests bring up a 403?

and ideas ~ I am on apache and unix...

thanks in advance.

jdMorgan

4:40 pm on Mar 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want both www and non-www requests to be handled by the first rule, then make the "www." optional.

Note that in this pattern and all others posted here, the "." should be escaped if you wish it to be matched as a literal period, and not as a regex token meaning "any single character."


RewriteCond %{HTTP_HOST} ^(www\.)?domain-1\.com(:80¦:443)?$

Optional port 80 and port 443 pattern added just in case you get one of these rare-but-valid requests.

Replace the broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 9:08 pm (utc) on Mar. 18, 2008]

loveunit

5:12 pm on Mar 18, 2008 (gmt 0)

10+ Year Member



Thanks for your reply, I've not tested your exact code, however I did try something similar before and this started the problem with the sub domains ( these are not real sub domains, but the typical ones made using my control panel - virtual directories really )

my second rule works fine for both www and non-www requests, however this is not possible on my main domain. do you know if this will still be fine?

thanks again for your help.

loveunit

8:31 pm on Mar 18, 2008 (gmt 0)

10+ Year Member



seems this might work, I added a condition which points the www and www-less address to the correct folder, and still allows the sub domains to work both as public URL's where required and URL's for file includes, for example css.domain-1.com:

# www.domain-1.com
RewriteCond %{HTTP_HOST} ^www.domain-1.com [OR]
RewriteCond %{HTTP_HOST} ^domain-1.com
RewriteCond %{REQUEST_URI} !^/folder/1/
RewriteRule ^(.*)$ folder/1/$1 [L]

# www.domain-2.com
RewriteCond %{HTTP_HOST} domain-2.com
RewriteCond %{REQUEST_URI} !^/folder/2/
RewriteRule ^(.*)$ folder/1/$1 [L]

not sure I can say exactly why, however seems to have a look to do with use of ^ and / or . before the domain name.

thanks for your help Jim.

jdMorgan

8:53 pm on Mar 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The modified code your posted does exactly the same thing as what I posted, but requires an extra line to do it.

[added] ...except that it had a typo in it. [/added]

Jim

[edited by: jdMorgan at 9:08 pm (utc) on Mar. 18, 2008]

loveunit

10:03 pm on Mar 18, 2008 (gmt 0)

10+ Year Member



less is more, and your code is by far more elegant than mine, so I will try it out also and see if I get the same result.. I guess it's a good thing that the coding syntax is flexible enough to allow several 'right' answers.

thanks again for sharing your knowledge.

g1smd

11:57 pm on Mar 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you need to test for both www and non-www note this simplification of the code snippet.

^(www\.)?domain-1\.com

Be sure to think about whether the dot needs to be inside the bracket or not, when you make this type of simplification.

loveunit

1:49 pm on Mar 31, 2008 (gmt 0)

10+ Year Member



Thanks all for previous answers - my .htaccess is up and running and now serves 4 domains - and the list is set to grow fast... which brings me to my next questions.

I have no domain management on my server - so I have no choice about using modrewrite - however could you tell me if this is a efficient and reliable way to handle multiple domains - I feel a little uneasy, but mostly because I still do not fully understand the syntax and rules.

also, I have a client with several domains with the same name but with different .tld's - I've searched but can't find the code I'd need to point all these domains to the sub directory - or would this be better handled using DNS configuration?

sorry to land all this at once - and thanks in advance for any advice.

jdMorgan

3:29 pm on Mar 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> now serves 4 domains - and the list is set to grow fast.
> I feel a little uneasy, but mostly because I still do not fully understand the syntax and rules.

Those two statements taken together indicate that you should address the second before attempting the first.

An efficient method is to define a subdirectory -- call it "users" or "sites" or "accounts" and put the content for the various domains in that subdirectory. You can then "map" domains and subdomains to that subdirectory by using the HTTP_HOST header. This covers the bulk of your hosted domains, although you might still need a few more rules to handle exceptions, such as your "main" domain if you want it stored apart from the "/sites" subdirectory.

There are dozens of different ways to do this, and you'll need to choose the method that best suits your needs. A simple example would be:


# Rewrite domains to subdirectories in "/sites" subdirectory
#
# Get requested domain, retaining subdomains except for "www"
# and dropping trailing period and/or port number if present
RewriteCond %{HTTP_HOST} ^(www\.)?(([^.]+\.)+[^.:]+)\.?(:[0-9]+)?$
# if not already rewritten to "/sites" subdirectory
RewriteCond %{REQUEST_URI} !^/sites/
# and if domain subdirectory exists in "/sites"
RewriteCond %{DOCUMENT_ROOT}/sites/%2 -d
# rewrite the domain to its subdirectory in "/sites"
RewriteRule ^(.*)$ /sites/%2/$1 [L]

As noted, this code drops the "www" subdomain if present in the request. Other subdomains are retained. It is possible that the HTTP_HOST variable will contain a trailing period and/or a port number. For example, "www.example.co.uk.:80" is a valid hostname. This code drops the trailing period and port number.

Since this code removes the "www" subdomain if present, each "site" should contain its own .htaccess file with a rewriterule to redirect non-www to www (or www to non-www) requests as desired by the "owner" of that domain.

Be very careful with FTP and shell file permissions: These must be restrictive to keep the "users" out of each others' filespace.

Jim