Webmasters Stack Exchange is a question and answer site for pro webmasters. 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 have a linux mail+http server located throguh Internet at www.mydomain.com. Domain was bought at GoDaddy so after configuring the server and getting the public ip address i went at GoDaddy's profile to modify the A and MXentries for my dns zone in order to point them to my server's public ip address. In the MX entry i did not placed the ip address but an alias called mail.mydomain.com.

Everything works fine but, as www.mydomain.com and mail.mydomain.com both are pointing to x.y.w.z where i have apache running, when you write www.mydomain.com or either mail.mydomain.com in the Internet Browser my default web page is shown. I would like to tell apache to only show the page if www.mydomain.com is written in the url and deny the access if mail.mydomain.com is written. Does anyone knows how sould i do it? Thanks :)

NameVirtualHost *:80

<VirtualHost *:80>
  ServerAdmin admin@mydomain.com
  DocumentRoot /var/www/html/mydomain.com/
  ServerName www.mydomain.com
  ErrorLog logs/mydomain_com-error_log
  CustomLog logs/mydomain_com-access_log common
share|improve this question

migrated from stackoverflow.com Sep 14 '11 at 18:37

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

up vote 2 down vote accepted

Just create another virtual host for mail.example.com, and you can tell Apache to do whatever you want when people go to that host.

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName mail.example.com
    ErrorLog logs/example_com-error_log
    # if you care about hits:
    CustomLog logs/example_com-access_log common

Then, if you want to forbid access entirely:

    <Directory />
        Order allow,deny
        Deny from all

Or to redirect to your real domain do this instead:

    RedirectPermanent / http://www.example.com/

Or you could do something else.

share|improve this answer
Thanks a lot for the tips :) – user846226 Sep 16 '11 at 8:55

If you put the following into your .htaccess every visitor coming to your site by means of any domain other than www.example.com will be forcedly redirected to the right domain:

RewriteEngine On
RewriteCond %{HTTP_HOST}   !^www\.example\.com$ [NC]
RewriteRule ^(.*)          http://www.example.com/$1 [L,R=301]
share|improve this answer
Thx a lot for the answer. Is it possible to achieve what i asked for just through a proper virtualhost configuration set? – user846226 Sep 14 '11 at 17:32
Although technically this works, it's inefficient for a couple reasons: first, mod_rewrite is pretty complex so it's best to avoid using it when you can do without it; also, enabling .htaccess files at all adds several filesystem accesses to every request. Besides, it's just weird organization to put a domain-level redirect in a directory context. – David Z Sep 14 '11 at 20:31
@David, all you say is true, unless you're on a shared hosting where you either way have .htaccess enabled. – sanmai Sep 15 '11 at 9:43

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.