以下是Apache2.4.1之后的版本禁止通过ip访问只能域名访问的方法,现在大多数Apache都是2.4.1版本之后的,继承了更多可启用的模块,如果你的Apache是2.4.1之前的版本,
在/etc/httpd/conf目录中vi httpd.conf 在文件末尾加入
#禁止所有非法域名
<VirtualHost *:80>
ServerName 服务器ip
<Location />
Order Allow,Deny
Deny from all
</Location>
</VirtualHost>
#允许访问的域名
<VirtualHost *:80>
DocumentRoot /alidata/www
ServerName www.你的域名 #带www
ServerAlias www.你的域名 #带www
<Directory "/alidata/www"> #改为自己的根目录
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /alidata/www
ServerName 你的域名 #不带www
ServerAlias 你的域名 #不带www
<Directory "/alidata/www"> #改为自己的根目录
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
加入以上代码不管域名带不带www都能访问,而直接通过ip访问则汇报403错误。