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 my.server.ip.address>
    DocumentRoot /home/user/public_html
    ServerName www.example.com
    ServerAlias example.com
    ErrorLog logs/example.com-error_log
  • 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, 2014 at 16:02
  • maybe the problem is in the firewall does not accept the ip port 80
    – jucaripo
    Jul 23, 2014 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, 2014 at 19:40
  • @closetnoc: Perhaps OP is worried someone will try entering the IP in a browser to figure out who it belongs to. If you're interested in staying anonymous, that'll stop most people who don't have the ability to get a court order to identify the owner of the IP. If someone is trying to access the site by its domain, obviously they already know whose site they're trying to access.
    – Sparkette
    May 2, 2015 at 6:32
  • @Dreamer Still- the point I was making, regardless of the protocol and header, the request is wrapped and addressed by IP address not by domain name. Because of that, you can block IP based requests, and I understand why someone wants to do this, however, you cannot divorce the domain name from the IP address. Which was the point I was trying to make.
    – closetnoc
    May 2, 2015 at 15:15

2 Answers 2


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:

    DocumentRoot /dev/null
    ServerName *
    Redirect 404 /

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.

  • 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, 2014 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
    – Tester
    Jul 23, 2014 at 16:44
  • 1
    Yes that is what I would expect if you did not setup NameVirtualHost correctly. Jul 23, 2014 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.

  • I tried this in .htaccess but still redirect me to my domain name.
    – Tester
    Jul 23, 2014 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, 2014 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.