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 am trying to use mod_rewrite to capture a subdomain, domain, and the arguments passed with the url.

I have multiple domains pointing to a single ip address, with both @ and * wildcard a-records, so that is all setup and ready.

so what I am trying to accomplish is: subdomain.domain.ext/arguments/argument/argument/

I want to capture: subdomain, domain.ext, and all the arguments after the / and place them into 3 separate variables so that the webpage served can differ based on this information.

Here is what I have so far:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([A-Za-z0-9-]+)\.domain\.com$ [NC]
RewriteRule ^([A-Za-z0-9-/]+)/?$ /redirect.php?subdomain=%2&args=$1 [QSA]

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

RewriteRule ^/?([a-zA-Z0-9/-]+)$ redirect.php?args=$1 [L]

This works for the following scenario:




however, I can't figure out how to capture domain.ext and use that as a variable.

Any help would be appreciated.

share|improve this question
Select / Highlight your code, press Control+K –  Tim Dec 30 '11 at 20:29
From our good friends @ Stackoverflow: stackoverflow.com/questions/49500/… –  Tim Dec 30 '11 at 20:32

Your Answer


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

Browse other questions tagged or ask your own question.