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.

My website's two major sections can be accessed using:

  • www.example.com for general content

  • laptop.example.com for more specific content

The laptop section is ready and deployed. User can access it directly. Also, search engines have started crawling and indexing it.

BUT, the general content is not ready; it will not be for few months.

As of now, there is no document root or files in the server for www and there is no virtual host tag for it.

Content of httpd.conf:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /path/to/website/subdomain/root
    ServerName example.com
    ServerAlias laptop.example.com
    <Directory /path/to/website/subdomain/root>
        AllowOverride All
        Allow from all
    </Directory>
</VirtualHost>

There is no other virtual host tag in there.

In my domain registrar control panel, I have added an A Record for the subdomain laptop and pointed it to my server IP. There are two other entries - @ and www both pointing at my IP. I am unable to edit/delete the www entry.

I thought that is all that is needed to prevent access to www. But it is not working!

When users type www.example.com, they are taken to laptop.example.com. I don't want this to happen. Just some default browser error message like Server not found or Page not found should be shown.

How can I achive this?


One Solution: I added the following to end of httpd.conf

<VirtualHost *:80>
    ServerName www.example.com
</VirtualHost>

Notice there is no document root! It worked. I am getting a 404 if I visit http://www.example.com or http://example.com. And the subdomains are working just fine.

I don't think this is an elegant method. Still waiting for a better/proper way to do it.

share|improve this question
    
Any global DocumentRoot will be inherited; this is generally not what you want. Set the DocumentRoot to something innocuous, like /tmp. You also don't seem to understand what ServerName does; set your Servername to www.example.com, and remove the ServerAlias. –  adaptr Jan 11 '13 at 15:36
    
@adaptr I changed the ServerName part. Also, if I set the DocumentRoot to /tmp or some other folder, then I am seeing the Apache Test Page instead of a 404. Is it possible to avoid that? –  user150932 Jan 11 '13 at 16:25
    
First, remove the ServerAlias. You don't need it. Second, that is likely some distro's defaulit rewrite kicking in; find it and remove it. –  adaptr Jan 14 '13 at 9:39

1 Answer 1

ServerName should be equal to the full domain name including the sub-domain laptop. Otherwise, the whole content will be reachable.

You may then create a block for the ServerName laptop.example.com and a other one with www.example.com with an authentication forced (users won't have the access unless they have the password) which block also google crawling. Or disabling totally www.example.com when you use only laptop.example.com

Users go to laptop.example.com when they go to www.example.com because by default the content of laptop.example.com is served for ServerName example.com which includes laptop.example.com and www.laptop.com. They would have the same content as long as both www and laptop are in the DNS.

ServerAlias is only when more than one domain name have to be redirected to the content defined in the block VirtualHost. For example, if you have laptop1 and laptop2 in the DNS and you want the same VirtualHost being applied for both.

Hoping this solves the issue you're having ...

EDIT to answer your comment:

I would have this in my config file:

<VirtualHost 192.168.0.103:80>

ServerName laptop
ServerAlias laptop laptop.example.com
DocumentRoot /var/www/laptop

</VirtualHost>

And then, if I want another content to be applied for a second VirtualHost named www, I would add a second paragraph similar to this one, but with different ServerName, ServerAlias, DocumentRoot. I just wanted to explain that you can do that with an authentication to make sure during development process you are the only one to access your VirtualHost

share|improve this answer
    
I added the full subdomain in ServerName like you suggested. Didn't work by itself. But I am not sure what you are suggesting in second paragraph. Are you asking me to add some content for www, add a virtual host tag for it, then add authentication to prevent access to it? –  user150932 Jan 11 '13 at 13:34
    
I have edited my answer to be able to put some code inside –  philippe Jan 11 '13 at 14:49
    
Oh I understand. But I forgot to say that, it will be months before even test content for www will be uploaded to the production server. I will also not be accessing it during this time. I just want to make it as if www.example.com simply didn't exist. –  user150932 Jan 11 '13 at 15:01
    
Ok, then simply add only one block for laptop and nothing for www. Anyway, is this configuration working, I mean, is your www disabled when trying to access www.example.com? –  philippe Jan 11 '13 at 16:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.