Forum Moderators: phranque

Message Too Old, No Replies

Allow access with domain.com but not by IP

         

veto

2:57 am on Aug 4, 2008 (gmt 0)

10+ Year Member



Here is the thing:

Whenever someone visits www.domain.com, it opens up index.html.

And when someone visits IP address 12.34.56.78, it opens up the same index.html.

(@) I want to disallow access by IP address and allow access only by the www.domain.com.

The problem is that www.domain.com is pointed to my IP address 12.34.56.78 with A record.

So, when I block my IP, the domain gets blocked message.

How can I do that (@)?

jdMorgan

3:28 am on Aug 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> So, when I block my IP, the domain gets blocked message.

How are you blocking your IP address? Can you post the code as a basis for discussion?

Jim

veto

3:01 pm on Aug 4, 2008 (gmt 0)

10+ Year Member



With htaccess allow deny

jdMorgan

4:17 pm on Aug 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The default variable tested by mod_access Allow and Deny directives is %{REMOTE_ADDR} or %{REMOTE_HOST}

If you wish to use mod_access to solve this problem, then you'll need to also use mod_setenvif to set a variable based on %{HTTP_HOST} instead, in order to deny access to the host (your server) based on its IP address. Something like:


SetEnvIf Host 192.168.76.54 block
Deny from env=block

This will return a 403-Forbidden response if an attempt is made to access your server using its IP address.

However, a better general-purpose solution is to use mod_rewrite to 301-redirect all requests to non-canonical hostnames to the canonical hostname. This solves the problem of IP addresses, missing "www," or incorrect or spurious subdomain requests, but does not simply reject the traffic as does the previous method, and so avoids the loss of potential visitors.

Assuming you already have other working mod_rewrite rules, one form of the mod_rewrite code for hostname canonicalization in .htaccess looks like this:


# if HTTP Host request header is non-blank
RewriteCond %{HTTP_HOST} .
# and requested hostname is [b]not[/b] exactly "www.example.com"
RewriteCond %{HTTP_HOST} !^www\.example\.com$
# redirect the request to the corresponding URL-path in www.example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

If you have no currently-working RewriteRules, you will need to preface the code above with either both of the following lines or only the second line -- It depends on your current server configuration:

Options +FollowSymLinks
RewriteEngine on

Jim