18

仅当来自 google(.com/.ru/.co.uk/.co.in/ 等)时,我才想.htaccess执行以下代码。http_referer这可能吗?

<filesMatch ".(jpg|jpeg|png|gif)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</FilesMatch>
4

2 回答 2

30

好吧,我发现您可以使用 mod_rewrite 以不同的方式设置标头,从而更容易:

RewriteCond %{HTTP_USER_AGENT} !(googlebot|bingbot|Baiduspider) [NC]  
RewriteCond %{HTTP_REFERER} google [NC]  
RewriteRule ^.*$ - [ENV=LONGCACHE:true]
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" env=LONGCACHE
Header set Pragma "no-cache" env=LONGCACHE
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT" env=LONGCACHE 
于 2013-04-18T05:42:58.890 回答
12

请注意,您可以将条件放入 Header 命令本身,以ap_expr格式(不需要 mod_rewrite):

Header set Pragma "no-cache" "expr=%{HTTP_USER_AGENT}=~/(googlebot|bingbot|Baiduspider)/i && %{HTTP_REFERER}=~/google/i"

(在您的特定情况下不是很有用,因为您需要添加 3 个标题)

于 2017-01-05T13:02:33.457 回答