1

我的 .htaccess 文件位于根目录中,并为多个网站提供服务。

它包含一个 RewriteEngine 块。

如果主机不是(www)www.example.com,我想阻止 Apache 评估规则:

RewriteEngine On
RewriteBase /

#If this is not about example.com, it is probably about our other websites, exit here
RewriteRule %{HTTP_HOST} !^(www|)example.com$ - [L]

但是,这会导致 500 内部错误。可能由于使用 {HTTP-HOST} 是 RewriteRule 指令。

所以我现在正在考虑这样的事情:

RewriteCond %{HTTP_HOST} !^(www|)example.com
RewriteRule (.*) $1 [L]

但这需要 Apache 重写并且不利于性能。

有人建议吗?

4

1 回答 1

2

如果主机不是(www) www.example.com ,我想阻止 Apache 评估规则

无论如何,您都在使用 mod_rewrite 。这样做的方法是跳过规则,如果它不是 www,所以你必须确保它是。

像这样的东西应该在根目录的 .htaccess 文件中工作:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)            /$1            [NC,L]

这与你所尝试的相反。

选项

根据Arkanon 的建议,此选项假定有更多域,并将规则仅应用于选定的域 (example.com),有或没有www.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^(.*)             /$1              [NC,L]
于 2013-03-24T11:04:37.087 回答