Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I've got a pretty unique issue (at least i've never seen it before).

I have an account on a shared host, where I host multiple domains.

So, domain A (the main acct) is the root folder on the wwwroot

domains B-D are subfolders of the root

I am currently using Coldfusion redirects to check cgi.server_name and send to the correct subfolder. But, I end up with DomainB.com/domainB/index.cfm

This causes problems for instance, if I want to send a user to DomainB.com/admin/ The server can't find it because the /domainB/ isn't in the path.

So I want to use mod-rewrite to address this issue. I thought I found a resolution with:

# If same website is accessible by different addresses, like domain.com, 
# www.domain.com and we want to redirect to one address.

RewriteCond %{HTTP_HOST} !^www.domainB.com$ [NC]
RewriteRule ^(.*)$ http://www.domainB.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www.domainB.com$
RewriteCond %{REQUEST_URI} !^/domainB/
RewriteRule ^(.*)$ /domainB/$1

This all seems pretty logical, and it works to an extent, BUT, every domain redirects to www.domainB.com which of course is not good. domainD is NOT happy when they try to go to their site and see www.domainC.com instead.

Anyone have an idea what i'm doing wrong, or better yet, how to make this dynamic? Assuming every subdirectory is named the same as the domain name. I.E. www.domainB.com will have a subdir of /root/domainB/

Thanks in advance.


Ok, this is what I have now (just appending to make sure it matches yours)

RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI}   [R=301,L,QSA]

RewriteCond %{REQUEST_URI} ^/domainB.com [NC]
RewriteRule /domainB/(.*) http://www.domainB.com/$1 [R=301,L,QSA]

But now, it redirects everything to the /root/ website or www.domainA.com

share|improve this question

3 Answers 3

This sounds like a perfect use for apache virtual sites. Do you have access to apache configuration files?

# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /wwwroot/
ServerName www.domainA.com

# Other directives here


<VirtualHost *:80>
DocumentRoot /wwwroot/domainb/
ServerName www.domainb.com    
# Other directives here



share|improve this answer
That would be a great solution if in fact I were in an Apache env. But no, it is Windows/IIS. Thanks though. –  Gene R Oct 25 '10 at 14:01
@Gene R If this really is IIS, then you're barking up the wrong tree. Mod_rewrite is an Apache module. IIS has a completely different URL rewriting engine. –  sysadmin1138 Oct 25 '10 at 22:54


#This first part checks for 'www' and appends it if its not there
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

#This next part is the magic that forwards to the appropriate subfolder including any
query string(s), etc.
RewriteCond %{HTTP_HOST} ^www.domainX.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domainX/
RewriteRule ^(.*)$ /domainX/$1

I thought this was it, but...

when I hit www.domainX.com/somedirectory/ The resulting directory is www.domainX/domainX/somedirectory/ So, the rewrite is not working as I hoped.

I want it to physically hit that directory on the server, but not display/relay that subdir to the user, in essence, I want that subdirectory to be invisible. Ideas?

share|improve this answer

Maybe you need to change your last line: from:

RewriteRule ^(.*)$ /domainX/$1


RewriteRule ^(.*)$ /$1

I'm sorry I don't have enough reputation to comment on your answer.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.