How to disable IP access

apache-2.2

How can I prevent visitors from accessing my site by IP? For example, my website uses IP 1.2.3.4, when they access http://1.2.3.4 directly, I want to redirect them to www.mywebsite.com or show them an error message.

I'm using Apache 2.2. Thanks!

Best Answer

LazyOne pointed out that you can do this with mod_rewrite (rewrite your IP to your domain name - there are lots of questions here that deal with writing rewrite rules).

I don't really see the value of doing this: Everyone accesses your website by IP address (after a DNS lookup turns your name into an IP), so the only real difference is what shows up in the Host: header, and you'll eat an extra HTTP request when the browser follows the redirect which makes things marginally slower.
If you need specific values in the Host: header for some reason though the rewrite rule solution will work for you.