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.

My site can be accessed from both

www.mysite.com/

http://my.ip.add.res/

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
1  
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

4 Answers 4

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 http://www.mysite.com URL. So any request for http://x.y.z.w/whatever will redirect to http://www.mysite.com/whatever. 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 http://www.webmasterworld.com/apache/3238805.htm.

# redirect to requested page in canonical domain

RewriteRule ^/(.*)$ http://www.mysite.com/$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 (www.mysite.com or mysite.com) 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']) != 'mysite.com') { 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

 
discard

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