如何在 .htaccess 中比较 HTTP_HOST 和 SERVER_NAME?

How to compare HTTP_HOST with SERVER_NAME in .htaccess?

我尝试了以下但似乎不起作用:

# IF HTTP_HOST != SERVER_NAME DO 403
RewriteCond %{HTTP_HOST} !%{SERVER_NAME} [NC]
RewriteRule .* - [F,L]

您不能在 RewriteCond 的 RHS 上使用变量。像这样使用它:

RewriteCond %{SERVER_NAME}#%{HTTP_HOST} ^(?:www\.)?([^#]+)#(?!(www\.)?).* [NC]
RewriteRule .* - [F,L]