您的虚拟主机缺少ServerName
,这将允许HTTP request hostname<->Virtualhost
映射。
因此,首先在虚拟主机上添加正确的Servername
。
然后,如果你想要所有其他名称呈现404你绝对需要一个默认的虚拟主机。这个默认的虚拟主机将使用HTTP请求中的无法识别的主机头(所以基于IP的访问,或与您的IP相关的其他DNS,或伪造的主机头)来捕获任何请求。
默认的虚拟主机是第一个在Apache配置中加载的,在类似debian的系统中,包含文件的前缀为00
,并且包含的字符按字母顺序加载以强制执行此状态。您可以始终使用-S选项来检查哪个虚拟主机是默认主机。
# debian-like
apachectl -S
# redhat like
httpd -S
然后如果你想所有这些访问默认的虚拟主机来呈现一个404,你只需让这个虚拟主机像一个很简单的一个404(总是一个好主意,删除所有有默认的废话)响应为eveything:
<Virtualhost *:80>
ServerName _default
# do not set that docroot as a parent of real doc root
# you do not want to let access to /var/www/mycompany as /company
# so create that directory
DocumentRoot /var/www/default
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access-default.log combined
<Directory />
Options FollowSymLinks
AllowOverride None
# apache<2.3 syntax
Order allow,deny
allow from all
</Directory>
# Now catch everything and throw a 404
# you'll need mod_alias for that (usually already there)
RedirectMatch 404 ^(.*)
</Virtualhost>
这不是针对HTTP请求的主机头操作进行绝对保护(他们还在奇怪的方式到你的虚拟主机绝对URI匹配和伪造的主机头),但是这是一个非常好的开始。
如果你想确保HTTP请求的HOST头部到达你的虚拟主机,那么你可以在最终的虚拟主机上添加一个mod_rewrite检查。
所以在你的例子中,对于目录leisure
,假设ServerName
应该是www.leisure.com
。在Directory
我会补充说:
<Directory /var/www/mycompany/websites/leisure>
Order deny,allow
Allow from all
Options FollowSymLinks
# not the right domain? -> 403
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.leisure\.com [NC]
RewriteRule .* - [F,L]
</Directory>