Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have some valid subdomains in my httpd.conf file. Now how do I redirect anything that's not a valid subdomain to the main website? What I mean is, if I have a valid subdomain hello.example.com, it should go to hello.example.com and if its blah.example.com which is not a valid subdomain, it should go to example.com. How can I make changes in my httpd.conf file to follow everything I just mentioned?

share|improve this question
1  
which server is that? –  Qwe Feb 24 '12 at 12:30
    
Sorry for late reply. It's Linux CentOS 64 bit using Apache(httpd) process. –  harithahv Feb 25 '12 at 13:09
add comment

migrated from stackoverflow.com Feb 24 '12 at 14:27

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

2 Answers

If you use Apache and if you have virtual hosts enabled, you got it automagically:

  • First virtualhost will be default host, which will serve all requests for hosts, not defined as virtual hosts
  • Known in Apache virtual hosts will be used on request

PS: hello.example.com and blah.example.com are hosts, not subdomains

share|improve this answer
    
Oh. Thanks for the help. I'm a newbie in this area, please forgive the mistake. I'm not sure about what you mentioned. –  harithahv Feb 25 '12 at 13:13
add comment

Setup a new default site in your apache-config and rewrite the url via .htaccess if this default site is called. Maybe something like /var/www/invalid-subdomain/.htaccess:

RewriteEngine on
RewriteBase /
RewriteRule ^$ http://example.com/ [R,L]

The above example is working well for my apache2 squeeze server.

share|improve this answer
    
Well.. Thanks for the reply. But I need to use this in the config file itself. There might be a rewrite rule to point wrong hosts(blah.example.com) to the root host(example.com) but I don't have any idea about it. –  harithahv Feb 25 '12 at 13:12
add comment

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.