Test environment:
CentOS Linux release 7.3.1611 (Core)
Apache: 2.4.6
If you are using Name Based on virtual hosts, usually you will want to prevent users from accessing your website directly using the IP address. The implementation method is as follows:
Create a new configuration file:
vim /etc/httpd/conf.d/block-ip-access.conf
The content is as follows:
<VirtualHost *:80> ServerName <your ip address> Redirect 403 / ErrorDocument 403 "Please use domain to access this site." DocumentRoot /dev/null/ UseCanonicalName Off UserDir disabled </VirtualHost>
Restart Apache:
systemctl restart httpd
After that, if you use IP to access directly, the browser will report a 403 error, and at the same time, "Please use domain to access this site." will be displayed on the page.
Note: If you are not using Name based virtual hosts and just resolve the domain name directly to the Apache server, you will also get a 403 error. That is, you need to have settings similar to the following:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com ... </VirtualHost>