htaccess redirects with regular expressions
Posted by David WhitehouseThe Problem
Ok so I hit a bit of a snag tonight, I had ended up with the following predicament.
I had a url: http://www.david-whitehouse.org/blog/ which I wanted to redirect to http://www.david-whitehouse.org/ but when I used the following code:
redirect 301 /blog/ http://www.david-whitehouse.org/
I ended up redirecting pages such as http://www.david-whitehouse.org/blog/blogging-voice-recognition/ to http://www.david-whitehouse.org/blogging-voice-recognition/ – 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…
Anyways!
The Solution
RedirectMatch 301 ^/blog/$ http://www.david-whitehouse.org/
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
Jaamit Durrani
Like this post? Subscribe to my RSS feed or receive updates via email.
Got something to say? Say it below
Related posts:
1 Comment »
RSS feed for comments on this post. TrackBack URI
Leave a comment
[...] it slowed my site down even further, which is pretty bad as it runs at a snail pace anyways – learn to edit your .htaccess file instead, it’s much more [...]
Pingback by WordPress Plugins to avoid LIKE THE PLAGUE MOFO!, by David Whitehouse — March 11, 2010 #