Idiosynchracy With Apache Rewrites

Posted on September 22, 2009, under GNU/Linux.

Did you know that it’s not possible to apply an Apache rewrite condition to multiple rewrite rules? I thought this would work, but requests from every IP address were being redirected:

RewriteCond %{REMOTE_ADDR} ^10\.0\.10\.10$
RewriteRule ^uploads/(.*)$ /wp-content/uploads/$1
RewriteRule (.*) http://deadorange.com/blog$1 [L,R=301]

It turns out that RewriteCond directives only apply to the following RewriteRule. So my rewrite directives above were equivalent to “If the request came from 10.0.10.10, rewrite /uploads/.* to /wp-content/uploads/.* .Next, redirect everyone to http://deadorange.com/blog$1 .”

Unfortunately, the only solution is to repeat the condition:

RewriteCond %{REMOTE_ADDR} ^10\.0\.10\.10$
RewriteRule ^uploads/(.*)$ /wp-content/uploads/$1
RewriteCond %{REMOTE_ADDR} ^10\.0\.10\.10$
RewriteRule (.*) http://deadorange.com/blog$1 [L,R=301]

That isn’t very DRY, but if it’s the only way, we have to live with it!

Share and Enjoy:
  • Twitter
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Print

2 Replies to "Idiosynchracy With Apache Rewrites"

gravatar

Chrono  on September 22, 2009

Actually, you can. Add the [C] flag to the first rule to chain it to the second one. If the first rule does not match (maybe due to its precondition) then all the chained rules are skipped.

gravatar

Nick  on September 22, 2009

Yeah, the [C] flag can help in certain situations. However, I needed the second RewriteRule to fire regardless of whether or not the first one matched.

Leave a Comment