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 want my website to be accessible only by the domain name, not by the server’s IP address. How can I configure Apache to achieve this?

Below are the VirtualHost configurations of Apache.

<VirtualHost *:80>
  ServerName not.configured
  DocumentRoot /var/www
</VirtualHost>

<VirtualHost my.server.ip.address>
    DocumentRoot /home/user/public_html
    ServerName www.example.com
    ServerAlias example.com
    ErrorLog logs/example.com-error_log
</VirtualHost>
share|improve this question
2  
The primary problem you have is that you cannot divorce the domain name from the IP address. All Apache traffic arrives addressed to the IP address with the domain name in the packet header. Apache receives the packet and then uses the address in the packet header to locate the proper website. There are things that can be done, however, I suspect your answer is in the .htaccess file more than anything. I do have a blank site that all packets that do not have a domain name in the packet header go to. I would have to think on this a bit. –  closetnoc Jul 23 at 16:02
    
maybe the problem is in the firewall does not accept the ip port 80 –  jucaripo Jul 23 at 16:06
1  
@closenoc: that's irrelevant for web traffic. All modern Browsers use HTTP 1.1, which demands a "host" - header. This header contains the server name that the client requested data from. This value is based on the URL that is requested, and is not translated via DNS. Thus, it differs between requesting a file through a domain name and requesting it through an IP address. And Apache can handle requests differently based on this distinction. –  Dreamer Jul 23 at 19:40

2 Answers 2

up vote 7 down vote accepted

Empty Virtual Host

With virtual hosting, all traffic is routed to an IP address and then Apache matches the hostname. When virtual hosting using NameVirtualHost is enabled, the site that responds to the IP address is the first one listed in the Apache configuration file.

So you can use a null virtualhost:

<VirtualHost 192.168.1.1:80>
    DocumentRoot /dev/null
    ServerName *
    Redirect 404 /
</VirtualHost>

I've not tested this so may need a tweak but if you put this first in your configuration then traffic to your IP address should return a 404 error.

You would then specify your VirtualHost second.

301 Redirect

Alternatively, you could just redirect requests to the IP address to the preferred domain using a 301 redirect in your htaccess.

See: What are the most commonly used and basic Apache htaccess redirects?

for a list of rewrite rules.

share|improve this answer
2  
Perfect! I have not checked your code of course, but it is not too far off from what I do. You explained it extremely well too! For those Apache installs that are not set-up for virtual websites (and there are some), this can be a bit trickier. That is what I was referring to with the .htaccess comment I made because it appeared that it was possibly a single site install- though I could be wrong. –  closetnoc Jul 23 at 16:30
    
I can not acess my website from both domain name and IP address after using the first solution. [warn] VirtualHost my.ip.address:80 overlaps with VirtualHost my.ip.address:0, the first has precedence, perhaps you need a NameVirtualHost directive –  Don Jul 23 at 16:44
1  
Yes that is what I would expect if you did not setup NameVirtualHost correctly. –  jeffatrackaid Jul 23 at 17:39

You could simply redirect the user to the named host:

# Uncomment the line below if not previously added in the file
# RewriteEngine On

# Rule to redirect to the named host
# Replace [xx.xx.xx.xx] woth your host's IP address
# Replace [yourdomain.com] with your host's proper URL
RewriteCond %{HTTP_HOST} ^xx\.xx\.xx\.xx$
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]

I didn't ttest this to see if it works or not, but at least it's an idea you can build on, for a proper solution.

share|improve this answer
    
I tried this in .htaccess but still redirect me to my domain name. –  Don Jul 23 at 16:40
    
You cannot break the link between your IP and your domain host - unless you're on a machine behind a router which only redirects port 80 from the public IP to your private IP on the subnet - but even so, requests comming on port 80 on the public IP will nevertheless be routed to your machine. This is because all requests are comming into the webserver from the IP - the domain name is just an "easy to remember" alias of your physical IP address. –  Thyamarkos Jul 23 at 16:59

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.