是否可以有条件地设置标题?

时间:2013-04-18 04:53:20

标签: apache .htaccess cache-control mod-headers

如果http_referer来自谷歌(.com / .ru / .co.uk / english / etc.等),我想htaccess执行以下代码。这可能吗?

<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>

2 个答案:

答案 0 :(得分:21)

好吧我发现你可以使用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 

答案 1 :(得分:6)

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

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

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