I have some mod rewrite rules that test for the presence of a file on various NFS mounts and I would like that the tests occur randomly, as a very rudimentary way to distribute load.

For example:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond /mnt/mount1/%1 -f
RewriteRule ^/(.+)$ /mnt/mount1/$1 [L]
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond /mnt/mount2/%1 -f
RewriteRule ^/(.+)$ /mount2/$1 [L]
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond /mnt/mount3/%1 -f
RewriteRule ^/(.+)$ /mnt/mount3/$1 [L]
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond /mnt/mount4/%1 -f
RewriteRule ^/(.+)$ /mnt/mount4/$1 [L]

As far as I understand mod_rewrite Apache will look for the file on /mnt/mount1, then mount2, mount3 and so on.

Can I randomize this on each request? I understand this is an odd request but I need a creative solution to some unforeseen downtime.

On a side note, do I need to redeclare RewriteCond %{REQUEST_URI} ^/(.+)$ each time like I have done?


2 Answers 2


As a suggestion, you can make a cron job that changes the file with the rewrites with other config file with the rewrite conditions in different order. You can run that cron job every minute and have different config files to replace it.

  • But I would have to constantly reboot Apache to do that, right?
    – Tom
    Jul 3, 2012 at 23:56
  • well, in the same script on the cron you can restart apache service. Jul 5, 2012 at 18:54
  • i'm not sure, but i think you can also put the rewrites in a .htaccess file. in that case you don't need to restart apache, just change the htaccess file Jul 5, 2012 at 18:55

I think I found a feature in mod_rewrite that can accomplish this: RewriteMap prg


  • RewriteMap rnd (randomized plain text) would be even better.
    – Andrew
    Jul 4, 2012 at 0:08
  • Are you sure? I need to check the presence of files (the files are not located on all the mounts - only some - and this must be checked. Apache then follows the first valid path). How would I do that with rnd?
    – Tom
    Jul 4, 2012 at 1:10
  • It depends how you state the question - the simple case of "random one of these values in a rewrite check" would use rnd, more complex checking could either modify the mapping file (possible race condition between read and change of state) or use an external program.
    – Andrew
    Jul 4, 2012 at 1:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .