Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a full-blown site like: (uses index.php)

I now want to have the possibility of setting up subsites like:

From , I understand that I have to do:

RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
RewriteCond %1 !=www
RewriteRule ^ index.php?domain=%1

But what about the other scripts like scriptA and scriptB? How do I tell httpd.conf to handle those properly as well?

How can I tell httpd.conf that handle everything after the 'forwardslash', exactly as it does on the main site, but pass a parameter flag like



I have lots of these subdomains being used, but I placed them _before_ the main 'www' one.

<VirtualHost IPADDRESS:80>
    DocumentRoot /home/test4/public_html
    UseCanonicalName On

<VirtualHost IPADDRESS:80>
    ServerAlias *
    DocumentRoot /var/www/html/beta
    UseCanonicalName On

<IfModule mod_geoip.c>
    GeoIPEnable On
    GeoIPDBFile /opt/GeoLiteCity.dat IndexCache

 <IfModule mod_rewrite.c>
    RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$$1 [R=301,NC,L]

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^(.*)${REQUEST_URI}?domain=%1 [L]

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteCond %{QUERY_STRING}  !^$
RewriteRule ^(.*)${REQUEST_URI}?%{QUERY_STRING}&domain=%1 [L]
share|improve this question
Don't cross post - it really doesn't help you. – Iain Jul 18 '12 at 11:17
What are you trying to achieve - multiple sites with the same content? Multiple sites with different content? Something else - please specify ? – Iain Jul 18 '12 at 11:20
Hi Iain - I'm trying to have multiple sites with different content, which is loaded based on the 'domain' parameter. – Steve Jul 19 '12 at 9:46
Thanks for the pointer MichelZ - that question is very close to what I need. I did try it out today, but it doesnt seem to redirect with the correct domain parameter, as well as it DOES rewrite the address in the browser (which is something I DONT want). – Steve Jul 19 '12 at 9:48
up vote 1 down vote accepted

If you want all of these URLs to end up at the same 3 scripts - as I surmise from your page-full of data - then you don't need any of this stuff.

Just rewrite to a relative URI instead:

RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
RewriteRule ^(.*\.php)$ $1?domain=%1 [QSA]

This will work for any URL.

I suspect you're making things horribly convoluted for no reason - judicious combination of vhosts and redirects will solve most problems without the need for complicated rewriting.

share|improve this answer
Hi adaptr - the VirtualHosts point to different test sites which are located in different folders. We're passing parameters via GET and POST and we're using rewritten URLs as well (eg: ) – Steve Jul 19 '12 at 11:27
I think this is working swimmingly! Doing more tests...! – Steve Jul 19 '12 at 11:32
Marvelous stuff! – Steve Jul 20 '12 at 12:08
Ah - little bit of a problem - this doesnt handle existing GET parameters at all... example, calls like: – Steve Jul 23 '12 at 12:37
Investigate the documentation for RewriteRule, especially the Flags part, where it says [QSA]. As for "handling" existing query strings - you cannot do that in a RewriteRule; add a RewriteCond and re-use its results. – adaptr Jul 23 '12 at 12:38

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.