我想在 Apache 的响应中添加一个“X-Slogan”标头,将其设置为从字符串列表中随机选择的值。到目前为止,我能想到的最佳解决方案是根据 TIME_SEC 轮换字符串,例如
UnsetEnv HEAD_X_SLOGAN_1
UnsetEnv HEAD_X_SLOGAN_2
UnsetEnv HEAD_X_SLOGAN_3
UnsetEnv HEAD_X_SLOGAN_4
RewriteCond %{TIME_SEC} <15
RewriteRule . - [env=HEAD_X_SLOGAN_1:%{TIME_SEC},last]
RewriteCond %{TIME_SEC} >14
RewriteCond %{TIME_SEC} <30
RewriteRule . - [env=HEAD_X_SLOGAN_2:%{TIME_SEC},last]
RewriteCond %{TIME_SEC} >29
RewriteCond %{TIME_SEC} <45
RewriteRule . - [env=HEAD_X_SLOGAN_3:%{TIME_SEC},last]
RewriteCond %{TIME_SEC} >44
RewriteRule . - [env=HEAD_X_SLOGAN_4:%{TIME_SEC},last]
Header set X-Slogan "Palm trees" env=HEAD_X_SLOGAN_1
Header set X-Slogan "Oranges" env=HEAD_X_SLOGAN_2
Header set X-Slogan "Shoes" env=HEAD_X_SLOGAN_3
Header set X-Slogan "Velociraptors" env=HEAD_X_SLOGAN_4
但是,这并不是真正随机的。有没有更好的方法来实现这一点?
答案1
mod_rewrite 有一个 rnd 的 MapType,它可以做你想做的事情。
看这一页在随机纯文本下。
答案2
感谢 Dennis,我找到了以下解决方案:
Apache 配置:
# Set a rotating slogan.
# Contents need to match the RewriteConds below.
RewriteMap slogans rnd:/web/www.wireshark.org/slogans.txt
RewriteRule . - [env=SLOGAN_NUM:${slogans:num}]
RewriteCond %{ENV:SLOGAN_NUM} =1
RewriteRule . - "[env=HEAD_X_SLOGAN:Slogan 1.,last]"
RewriteCond %{ENV:SLOGAN_NUM} =2
RewriteRule . - "[env=HEAD_X_SLOGAN:Slogan 2.,last]"
RewriteCond %{ENV:SLOGAN_NUM} =3
RewriteRule . - "[env=HEAD_X_SLOGAN:Slogan 3.,last]"
RewriteCond %{ENV:SLOGAN_NUM} =4
RewriteRule . - "[env=HEAD_X_SLOGAN:Slogan 4.,last]"
Header set X-Slogan "%{HEAD_X_SLOGAN}e"
口号.txt:
#
# Randomized slogan numbers
# Need to match the slogans in the Apache config.
#
num 1|2|3|4