简体   繁体   English

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

[英]Is it possible to set headers conditionally?

I would like .htaccess to perform the following code ONLY if http_referer is from google (.com/ .ru/ .co.uk /.co.in/ etc.).如果http_referer来自谷歌(.com/.ru/.co.uk/.co.in/ 等),我希望.htaccess执行以下代码。 Is this possible?这可能吗?

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

Well I figured out you can set headers a different way using mod_rewrite making it much easier:好吧,我发现您可以使用 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 

Note that you can put the condition in the Header command itself, in ap_expr format (does not require mod_rewrite):请注意,您可以将条件以ap_expr格式(不需要 mod_rewrite)放在 Header 命令本身中:

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

(not very useful in your particular case since you need to add 3 headers) (在您的特定情况下不是很有用,因为您需要添加 3 个标题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM