Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have added a CNAME record to my DNS on my CentOS server to redirect subdomain.mydomain.com to another server.

I also use mod_rewrite to rewrite mydomain.com to www.mydomain.com:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

this is placed into .htaccess in the root public html directory

Now, the problem is that whenever I lookup subdomain.mydomain.com it redirects me to www.mydomain.com

I tried adding another RewriteCond:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^subdomain\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

But it didn't help...

Anyone has some ideas?

share|improve this question
Is the CNAME record for subdomain.mydomain.com pointing to either mydomain.com or www.mydomain.com? Or is it pointing to some other domain entirely? If I understand your description correctly subdomain should never get to the mod_rewrite stuff. That means the problem is with the dns. –  matthew Feb 6 '11 at 2:04
the CNAME record is pointing to another domain entirely –  Tom Feb 6 '11 at 2:07
I just ran dig @ns1.mydomain.com subdomain.mydomain.com - the answer was correct from the dns –  Tom Feb 6 '11 at 2:17
If you turn on rewrite logging, what do you see in the log file? –  larsks Feb 6 '11 at 2:37
Do you have any /etc/hosts entries that may be causing problems? Try the command getent hosts subdomain.mydomain.com. –  BillThor Feb 6 '11 at 5:11

2 Answers 2

Maybe a redirect is happening in the web application.

  • Try download static file(css,js,jpg,...) from subdomain.mydomain.com
  • Use RewriteLog and RewriteLogLevel Directive for debug
share|improve this answer
up vote 0 down vote accepted

matthew was right, the request should never get to mod_rewrite with proper dns alias configuration. The problem was in a simple DNS record syntax mistake made accidentally when typing. It's now taken care of and working properly :)

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.