Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

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
add comment

2 Answers

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
add comment

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

share|improve this answer
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 '11 at 16:40
    
you're absolutely right! –  CMag Aug 16 '11 at 21:21
add comment

Your Answer

 
discard

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.