使用 Apache,我尝试设置一条重写规则,该规则将包含在多个虚拟主机中。实际上,此规则文件包含在 2 个 vhost 中。
第一个 vhost 仅包含此重写规则(并且它可以工作)并且充当 Nginx 的反向代理:
RewriteEngine On
Include /etc/apache2/bad-referers
另一个包含带有 W3 Total Cache 插件的 Wordpress、其 Apache 配置和默认的 WP .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.tld$
RewriteRule ^(.*) http://domain.tld/$1 [R=301,L,QSA]
Include /etc/apache2/bad-referers
Include /var/www/d/domain.tld/w3tc.conf
在这种情况下,bad-referers 永远不会被匹配,而此规则以 [F,L] 结尾。域名重写和 W3 Total Cache 规则运行良好。我真的不明白。
Include 指令的顺序或位置重要吗?我是否必须将所有这些文件的内容放在 vhost 指令中?还是 RewriteBase 中?
通过 /server-info 获取(几乎)完整的虚拟主机:
在文件中:/etc/apache2/sites-enabled/domain.tld
1: <VirtualHost *:80>
7: <Directory /var/www/d/domain.tld/html/>
14: RewriteEngine On
16: RewriteCond %{HTTP_HOST} !^domain.tld$
17: RewriteRule ^(.*) http://domain.tld/$1 [R=301,L,QSA]
在文件中:/etc/apache2/bad-referers
2: RewriteEngine on
5: RewriteCond %{HTTP_REFERER} 4webmasters\.org [NC,OR]
104: RewriteCond %{HTTP_REFERER} youporn-forum\.uni\.me [NC]
105: RewriteRule ^.* - [F,L]
在文件中:/var/www/d/domain.tld/w3tc.conf
191: RewriteEngine On
192: RewriteBase /wp-content/cache/minify/
193: RewriteRule /w3tc_rewrite_test$ ../../plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 [L]
194: RewriteCond %{HTTP:Accept-Encoding} gzip
195: RewriteRule .* - [E=APPEND_EXT:.gzip]
196: RewriteCond %{REQUEST_FILENAME}%{ENV:APPEND_EXT} -f
197: RewriteRule (.*) $1%{ENV:APPEND_EXT} [L]
198: RewriteRule ^(.+/[X]+\.css)$ ../../plugins/w3-total-cache/pub/minify.php?test_file=$1 [L]
199: RewriteRule ^(.+\.(css|js))$ ../../plugins/w3-total-cache/pub/minify.php?file=$1 [L]
207: RewriteEngine On
208: RewriteBase /
209: RewriteCond %{HTTP:Accept-Encoding} gzip
210: RewriteRule .* - [E=W3TC_ENC:_gzip]
211: RewriteCond %{REQUEST_METHOD} !=POST
212: RewriteCond %{QUERY_STRING} =""
213: RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
214: RewriteCond %{HTTP_USER_AGENT} !(W3\ Total\ Cache/0\.9\.3) [NC]
215: RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index.html%{ENV:W3TC_ENC}" -f
216: RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index.html%{ENV:W3TC_ENC}" [L]
In file: `/etc/apache2/sites-enabled/domain.tld`
: </Directory>
: </VirtualHost>
答案1
使用另一种方法 SetEnvIf 和访问拒绝解决。
<IfModule mod_setenvif.c>
SetEnvIfNoCase Referer 4webmasters\.org spambot=yes
Order allow,deny
Allow from all
Deny from env=spambot
</IfModule>