1

I have the following snippet:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://%{SERVER_NAME}/
RewriteRule \.(js|css|png|jpg) - [R=404,L]

Simple and should work right? It seems to 404 the listed filetypes if I have referrers enabled on browser. Disabling referrers it then allows the files to be served. I have checked the value of %{SERVER_NAME} and it is www.mydomain.com I've tested this in multiple browsers and under HTTP and HTTPS, all have the same result. I used the below rewrite to check %{SERVER_NAME}'s value:

RewriteRule servername value_is_%{SERVER_NAME} [R=301,L]

The URL I get redirected to is then https://www.mydomain.com/value_is_www.mydomain.com

That being said the snippet should allow a referrer with that value or an empty one. But why is it being triggered? It's been driving me nuts for the past 2 hours, but it's 5am so I could be just crazy =o\ Thank you in advance, and I'm off to bed!

4

1 に答える 1

1

問題は、サーバーの起動時にパターンがプリコンパイルされるため、条件付きパターンで変数を使用できないことです(少なくとも、Apache 2.4までは)。

ただし、特定の問題については、状態を模倣するために使用できる簡単な回避策があります。

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{SERVER_NAME}%{HTTP_REFERER} !^(.*)https?://\1/
RewriteRule \.(js|css|png|jpg) - [R=404,L]

うん、それだけです。変数は使用できませんが、後方参照は使用できます。

ああ...そしてところで。Apache 2.4には、条件付きパターンの代わりに使用できる式が付属しています。

 RewriteCond expr "! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"
于 2012-11-10T22:25:15.250 に答える