htaccess redirects with regular expressions

The Problem

Ok so I hit a bit of a snag tonight, I had ended up with the following predicament.

I had a url: which I wanted to redirect to but when I used the following code:

redirect 301 /blog/

I ended up redirecting pages such as to – but I wanted to keep it where it was (plus this created a redirect loop, which I could have fixed).

Twitter Friends to the Rescue!

So I asked for help on Twitter – offering a free link for the right answer (at first a site wide, then I chickened out and said “just a post”). Thankfully I found the answer – so they can all settle for a nice link in this post (see below).

Many things were suggested, some would have worked but I felt they were workarounds and I wanted to solve this particular issue in this way. I had tried some regular expressions, others passed me some more and people were passing me mod_rewrite code too. Unfortunately none of it seemed to work, some gave 500 errors, where as others resulted in the page sitting their, not moving – laughing at me, mocking at me, whispering…


The Solution

RedirectMatch 301 ^/blog/$

redirect doesn’t let you use regular expressions – RedirectMatch does (perhaps this is common knowledge, but I couldn’t find it anywhere and it wasn’t in the code anyone gave me). I found the fix here.

Thanks guys

For those that participated in helping me, I would like to thank (in no particular order):
Malcolm Coles
David Elstob
Will Critchlow
David Cumbor
Teifion Jordan