4

I would like to add an "X-Slogan" header to Apache's responses set to a value chosen at random from a list of strings. The best solution I've been able to come up with so far is to rotate through the strings based on TIME_SEC, e.g.

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

However, this isn't really random. Is there a better way to do this?

Wesley
  • 32,690
  • 9
  • 82
  • 117
Gerald Combs
  • 6,441
  • 25
  • 35

2 Answers2

2

mod_rewrite has a MapType of rnd which may be able to do what you want.

See this page under randomized plain text.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
1

Thanks to Dennis I arrived at the following solution:

Apache config:

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

slogans.txt:

#
# Randomized slogan numbers
# Need to match the slogans in the Apache config.
#
num 1|2|3|4
Gerald Combs
  • 6,441
  • 25
  • 35
  • I'm glad that worked for you. I had kind of envisioned the slogans being in the text file instead of indexes and you'd just have one rewrite rule. – Dennis Williamson Dec 01 '09 at 23:16
  • I tried adding them to the file initially. It only parsed up to the first space, even when wrapped in quotes. There's no doubt a cleaner way to do this but I'm happy that it works. – Gerald Combs Dec 02 '09 at 01:10
  • You might try escaping the spaces instead of using quotes: `slogan Slogan\ 1|Slogan\ 2` – Dennis Williamson Dec 03 '09 at 04:02