Forum Moderators: phranque
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.
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)?$
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]
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.
# 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.
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.
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]
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