在htaccess中的filesmatch中使用HTTP主机

时间:2018-03-23 06:22:10

标签: apache .htaccess

我正在尝试根据主机匹配我的站点地图,但它似乎不起作用。你能帮我搞清楚吗?

我的站点地图是sitemap-localhost.xml,因为我在本地运行

我试过了

<FilesMatch (sitemap-%{HTTP:Host}.xml)>
Order Deny,Allow
Allow from all
</FilesMatch>

<FilesMatch "sitemap-%{HTTP:Host}.xml">
Order Deny,Allow
Allow from all
</FilesMatch>

<FilesMatch sitemap-%{HTTP:Host}.xml>
Order Deny,Allow
Allow from all
</FilesMatch>

但似乎没有任何效果。问题是我有两个域指向服务器中的同一文件夹,这两个域有两个站点地图。

1 个答案:

答案 0 :(得分:1)

FilesMatch指令旨在仅与文件匹配。您无法使用此指令检查HTTP_HOST标头或URL路径,只允许在模式中使用带扩展名的文件名。

如果您想拒绝访问特定主机的xml文件,例如要拒绝访问thishost.com/sitemap.xml,您可以使用mod-rewrite`。

RewriteEngine on

RewriteCond %{HTTP_HOST} ^thishost\.com$
RewriteRule ^/?sitemap\.xml$ - [R=403,L]

如果客户访问thishost.com/sitemap.xml,则会向客户返回403错误。

上述模式中的前导斜杠(/?)是可选的,因此规则可以在上下文htaccessserver.config中使用。

相关问题