4

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!

2
  • You can do such redirect to a domain name with mod_rewrite.
    – LazyOne
    Aug 15, 2011 at 16:30
  • @lastsmoke can you post the link and we'll close this one as a duplicate? Thanks!
    – voretaq7
    Aug 15, 2011 at 17:09

2 Answers 2

3

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.

1
  • Also i need to say the people that there a bunch of third party services that deal and fix this kind of things with a lot of additional layers of security like CloudFlare Jun 27, 2021 at 19:31
1

Aside from reading here: http://httpd.apache.org/docs/2.0/mod/mod_access.html

You can always drop via tcpwrappers /etc/hosts.deny or iptables :P

2
  • 1
    I think you misread the question (or I did) -- it sounds like they want to prevent people from accessing a site hosted on 1.2.3.4 by doing 1.2.3.4 (forcing them to use mysite.com and rewriting 1.2.3.4 appropriately if it's used).
    – voretaq7
    Aug 15, 2011 at 16:40
  • you're absolutely right!
    – CMag
    Aug 16, 2011 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .