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

I have a full-blown site like:

http://www.example.com (uses index.php)
http://www.example.com/scriptA.php
http://www.example.com/scriptB.php

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

http://alpha.example.com
http://alpha.example.com/scriptA.php
http://alpha.example.com/scriptB.php

From http://stackoverflow.com/questions/2844004/subdomain-url-rewriting-and-web-apps/2844033#2844033 , 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

&domain=alpha

EDIT1

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

<VirtualHost IPADDRESS:80>
    ServerAlias test4.example.com
    ServerAdmin me@me.com
    DocumentRoot /home/test4/public_html
    ServerName test4.example.com
    UseCanonicalName On
</VirtualHost>



<VirtualHost IPADDRESS:80>
    ServerAlias *.example.com
    ServerAdmin me@me.com
    DocumentRoot /var/www/html/beta
    ServerName example.com
    UseCanonicalName On

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

 <IfModule mod_rewrite.c>
    RewriteEngine On

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

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

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteCond %{QUERY_STRING}  !^$
RewriteRule ^(.*)$   http://www.example.com%{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

1 Answer 1

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: www.example.com/event/1234/some-text-here ) –  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: alpha.example.com/index.php?asdf=123&def=456 –  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

 
discard

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.