Mobile app version of vmapp.org
Login or Join
LarsenBagley505

: How can I disallow access to my website by its IP address? 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

@LarsenBagley505

Posted in: #Apache #Htaccess

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 example.com ServerAlias example.com
ErrorLog logs/example.com-error_log
</VirtualHost>

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @LarsenBagley505

2 Comments

Sorted by latest first Latest Oldest Best

 

@Margaret670

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 (.*) yourdomain.com/ [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.

10% popularity Vote Up Vote Down


 

@Becky754

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme