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 would like to redirect my wildcard subdomain to my main domain using the subdomain as a GET parameter in the URL using .htaccess

Example of what I would like to happen:

redirects to

Here is the current rule I have put together based on other answers on this site. However, when the redirect happens I get the following URL with a "Redirect Loop" error:

RewriteCond %{HTTP_HOST} ^(.*)
RewriteRule ^(.*)$ [R,L]

I've seen this answered a few different ways on this website but unfortunately none of them worked for me. Any help would be appreciated. Thank you

Details: I'm using PHP, and i have setup * as a subdomain in my hosting panel.

share|improve this question

Your redirect loop is happening because you are redirecting everything (even Maybe try something like:

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

This should only fire when you're on something other than

share|improve this answer
Hi Seth. I tried that code you gave me but it doesn't appear to redirect the page. When I enter it just stays on the main homepage and the URL says – justinl Sep 21 '09 at 21:49
up vote 0 down vote accepted

After trying to solve this using .htaccess and failing, I ended up using PHP to do the URL parsing for me. For those who wish to see what I did:

$host = $_SERVER['HTTP_HOST'];
$subdomain = str_replace('', '', $host);
if($subdomain != 'www')
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.