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

Is there a .htaccess variable I can use which excludes the subdomain?

For example, if the domain is, how can I use in my .htaccess file?

I need to use a variable. I can't hard code

share|improve this question
Can you clarify what you're attempting to achieve? What kind of configuration directive in the htaccess file are you looking to use this in? – Shane Madden Nov 19 '12 at 5:41
Hi Shane. I have a very complex domain structure I use for about 150 websites. When people go to I need to redirect them to The problem is I don't know which domain they are using, so I need to use a server variable to redirect to Basically I just need a server variable which excludes the subdomain portion. – Steve Nov 19 '12 at 5:44
Are you looking to use it in a RewriteRule? Or your PHP code? What exactly you'll be doing to redirect is relevant to how this can be accomplished. – Shane Madden Nov 19 '12 at 5:45
Hi Shane. It's a RewriteRule in a htaccess file. For example RewriteEngine On RewriteCond %{HTTP_HOST} ^m\.%{DOMAIN_WITHOUT_SUBDOMAIN}$ RewriteRule ^(.*)$ %{DOMAIN_WITHOUT_SUBDOMAIN}/app/controllers/mobile/index.php [L] PS Sorry for crappy formatting, the two spaces for a new line thing doesn't seem to work. – Steve Nov 19 '12 at 5:51
up vote 1 down vote accepted

You've pretty much got it - the trick is that you can use matches from the RewriteCond in your RewriteRule.

So, to redirect a request to to

# We'll capture the last two period-separated sections
# (as well as ensure that at least 3 sections were in the requested hostname)
RewriteCond %{HTTP_HOST} \.([^\.]\.[^\.])$
# Now we can use that captured section (the last two parts of the name) with %1:
RewriteRule ^.*$ http://%1/script.php [R=301,L]
share|improve this answer
Ah! So simple. Thank you Shane. – Steve Nov 19 '12 at 6:19

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.