Forum Moderators: phranque
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 (@)?
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
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]
Options +FollowSymLinks
RewriteEngine on