Posted on Jul 18, 2008

mod_rewrite RewriteRule flag ordering and WordPress

I spent about an hour yesterday looking up URL’s on my site that have been archived or moved over the years and redirecting them via .htaccess to the appropriate sections. The problem was, I could only get proxy rules to work within WordPress.

I kept trying the following, which I assumed would work after reading the apache mod_rewrite documentation:

RewriteRule ^gallery/$ /photos/ [R=301,L]

I finally gave up and went with a proxy rule that worked fine, but wasn’t exactly what I wanted (because the url didn’t get changed — ie, proxy).

RewriteRule ^gallery/$ /photos/ [P]

Just a second ago though, I thought to myself, “I wonder if the order of the flags plays a role.” And of course, it does… though I can’t find any documentation to support this.

So if you’re driven nuts trying to create a custom rewrite rule which won’t work for you, remember that order matters. Give the rule the last flag first, then the redirect flag.

RewriteRule ^gallery/$ /photos/ [L,R=301]

I suppose that might make a lot of sense to mod_rewrite veterans but not for me. I’m very glad I figured this out and hope this post can help someone (or me again) in the future.