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.

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: forum.mydomain.com. Now, it does not exists anymore. A person who access my site by forum.mydomain.com should be redirected to mydomain.com.

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} !^mydomain.com$ [NC] RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]

if the host is not mydomain.com, redirect to mydomain.com. Shouldn't forum.mydomain.com 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 mydomain.com ( !^mydomain.com$ ) redirect to http://mydomain.com.

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: "http://mydomain.com/http://www.mydomain.com/%5Brequested file]"?

Thank a LOT to everyone.

share|improve this question

migrated from stackoverflow.com Nov 12 '09 at 18:01

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

1 Answer 1

up vote 4 down vote accepted
RewriteBase / 
RewriteCond %{HTTP_HOST} !^mydomain.com$ [NC] 
RewriteRule ^(.*)$ http://mydomain.com/$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 forum.mydomain.com, but only if forum.mydomain.com 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 mydomain.com.

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

 
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.