Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


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?

share|improve this question
up vote 2 down vote accepted

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

See this page under randomized plain text.

share|improve this answer
Thanks! A rnd MapType worked! – Gerald Combs Dec 1 '09 at 22:45

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


# Randomized slogan numbers
# Need to match the slogans in the Apache config.
num 1|2|3|4
share|improve this answer
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 1 '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 2 '09 at 1:10
You might try escaping the spaces instead of using quotes: slogan Slogan\ 1|Slogan\ 2 – Dennis Williamson Dec 3 '09 at 4:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.