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

My site can be accessed from both


I would prefer only the first method. Can I prevent my website from being accessed by IP ?

I am using Litespeed and I can configure it with .htaccess files.

share|improve this question
webserver? OS? these will make a difference in the answer – David Pashley Oct 22 '09 at 21:28
You'll need to provide more details, primarily the type of http server and whether or not you have access to configure it. – Insyte Oct 22 '09 at 21:28
Question 1) Why??... Question 2) What web server are you using? – squillman Oct 22 '09 at 21:29
Made the edits, sorry for not being clear. – user23735 Oct 22 '09 at 21:34
squillman, I am hoping to prevent duplicate content. – user23735 Oct 22 '09 at 21:36

The simple answer for this is to have a default virtualhost redirect to one of your other virtual hosts. Unfortunately I don't know how to do this with litespeed, but with apache, you would have several virtualhosts, with the default listed first and other name based virtual hosts listed after that. When the Host: header matches one of the virtual hosts, it will be served by those. When you connect via the IP address, it won't match one of the names in the virtual hosts and will use the first one. In that virtualhost, you have a redirect to the hostname you would prefer users to connect to.

This does require clients to use HTTP 1.1, but it's a rare client that doesn't.

share|improve this answer

Your site will always be accessible via its IP address whenever a browser makes requests for an http://x.y.z.w URL. However, using mod_rewrite (Apache) you can intercept every http request and redirect to an URL. So any request for http://x.y.z.w/whatever will redirect to If the site is Internet accessible and Google indexing is important to you, you'll want to issue a 301 (permanent) redirect so the pages are indexed by name.

There's an example of the rewrite rule at

# redirect to requested page in canonical domain

RewriteRule ^/(.*)$$1 [R=301,L]

share|improve this answer

You would require host headers on your web server. That way it requires the browser to send a valid host header ( or to retrieve the site. This still exposes the web server to IP based queries because the port is still open, but it should not respond with content.

share|improve this answer
Something like if ($_SERVER['SERVER_NAME']) != '') { exit();} ? – user23735 Oct 22 '09 at 22:04
+1 Name Based Virtual Hosting seems like a good solution to me. Set the default to a site with no content, and have a virtual host for for the name you want. – Zoredache Oct 22 '09 at 22:05

If you are on a Linux server you can use iptables :

iptables -I INPUT -d YourIP -p tcp --dport 80 -m string --to 700 --algo bm --string 'Host: YourIP' -j DROP

And your webserver will not respond.

share|improve this answer
but you'll tie up your server resources until the connection times out. You will also block the client, which might be considered rude. – David Pashley Oct 22 '09 at 21:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.