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

short story: I'm new to apache (and sysadministration in general) and trying to redirect a subdomain to my main domain. I already have a rewrite rule that get rid of the 'www.' subdomain.

long story: My old site went down. I rebuilded it. There was a subdomain to that old site: Now, it does not exists anymore. A person who access my site by should be redirected to

I'm using .htaccess file. That's what my hoster allows me to use.

Here's what in it: RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^$ [NC] RewriteRule ^(.*)$$1 [L,R=301]

if the host is not, redirect to Shouldn't fit in the condition?

I know it's probably been asked, but I did not found any question where they are in my situation. And since I already have a hard time understanding the rule above, I'd prefer to have someone who knows wants he talks about than me doing hit and miss.

Now for the optional part of the question, if someone could explain to me the rule above, it would be great.

Here's what I understand in the above rule:

if the host is NOT ( !^$ ) redirect to

What I don't understand is the first part of the rule regex: ^(.*)$, and then the reference to it ($1). How come matching everything can be put as the requested file path? Wouldn't it do something like that: " file]"?

Thank a LOT to everyone.

share|improve this question

migrated from Nov 12 '09 at 18:01

This question came from our site for professional and enthusiast programmers.

up vote 5 down vote accepted
RewriteBase / 
RewriteCond %{HTTP_HOST} !^$ [NC] 
RewriteRule ^(.*)$$1 [L,R=301]

The above code looks at the HTTP host of the connecting browser. Note that .htaccess can only work on a request that can reach the webserver. This will work for, but only if actually loads the content in the directory that the .htacess file resides in. If you do not have the DNS records set up (for example, a * A record or a "forum" A record) then it will not even go to the site at all, because the DNS is invalid. Also, Apache must be set up to load the site for that VirtualHost. So it must be listed in the Apache configuration as well before .htaccess can forward the URL as specified.

For your second question, in the RewriteRule part ^(.*)$, this only matches the file that is accessed off the server. The RewriteCond part only applies the rule on certain hostnames, then ^(.*)$ specifies that any request follow the rewrite rules.

So the above .htaccess code will apply for all requests after the / if the host does not equal

Let me know if you need any further explanation.

share|improve this answer
It does answer a lot of my questions. Thanks a lot for your time. I need 15 rep to vote you up, so sorry, I can't right now. – Jean-Francois Nov 13 '09 at 20:40

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.