migswd migswd - 4 months ago 24
Apache Configuration Question

Disable browser access to server IP address

I have a server that I can access in browser through the domain name (secured with SSL) and the server IP address.

I would like to disable access with the server ip address:

http://123.45.678.901/ and https://123.45.678.901/

How can I do that ?


OK found the complete solution here via htaccess file and 2 rules :

RULE 1: Redirect all requests to secure HTTPS access (including ip request http://123.45.678.901 )

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://domain.com/$1 [L,R=301,QSA]

RULE 2: If the domain or subdomain is not exactly domain.com redirect to bare domain (mandatory to catch https://123.45.678.901/any-page for example)
Using redirect all wildcard subdomains to root domain

RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule (.*) https://domain.com/$1 [L,R=302,QSA]

All tests results here :

http://123.45.678.901/ > now redirect to https://domain.com/  
https://123.45.678.901/ > now redirect to https://domain.com/  
http://123.45.678.901/any-page > 404 > https://domain.com/index.php?p=any-page  
https://123.45.678.901/any-page > 404 > https://domain.com/index.php?p=any-page