Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

I'm using Apache 2.2. Thanks!

share|improve this question
You can do such redirect to a domain name with mod_rewrite. – LazyOne Aug 15 '11 at 16:30
Thanks, found a discussion with detailed instructions. – lastsmoke Aug 15 '11 at 16:53
@lastsmoke can you post the link and we'll close this one as a duplicate? Thanks! – voretaq7 Aug 15 '11 at 17:09

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.

share|improve this answer

Aside from reading here:

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

share|improve this answer
I think you misread the question (or I did) -- it sounds like they want to prevent people from accessing a site hosted on by doing (forcing them to use and rewriting appropriately if it's used). – voretaq7 Aug 15 '11 at 16:40
you're absolutely right! – CMag Aug 16 '11 at 21:21

Your Answer


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.