Feb 01, 2009
301 Redirect and RedirectMatch
There is a lot of information available on the net for how to use a 301 redirect in a .htaccess file.
But, largely it all tells you how to do the same 4 or 5 things. What if I needed something different? SOL, baby.
Recently I worked on migrating an old ASP based website to a new WordPress installation. Everything went pretty well, except that the old site was pretty darn popular and there are literally thousands of links to it all over the net. We didn’t want to lose those links and dump them all into an ugly 404 not-found error page, but yet given the fairly sweeping changes that were made, there was no real way to do a one-to-one mapping from the old pages to the new.
As a result, we wanted to take any ASP page call and simply redirect it to the root of the site. There is an added challenge in that the site actually has multiple domains pointing to it, which added a bit of complexity. I had tried modrewrite changes, but they didn’t seem to be working very well. It also seemed a bit heavy handed, when what I really wanted to do was a “simple” redirect.
As you may know, there is a mechanism to do that, and it is simply called Redirect, and it works as follows:
Redirect 301 oldfile newfile
Unfortunately, the syntax only works for explicit matches, meaning that you have to specify EVERY .asp page and explicitly point it to another location. UGH. Not good for a site with well over a hundred pages of content. Even if I was willing to type them all in, since we wanted to redirect everything to the root anyway, adding all of these redirects would be huge waste of processing overhead.
What to do? After reading way too much information online and slogging through the Apache docs for a few hours, I stumbled across RedirectMatch, a great way to add a little regular expression logic to your redirect calls.
Using this simple tool, I was easily able to redirect ANY .asp file to the root.
RedirectMatch 301 (.*).asp /
Simple. Elegant. No modrewrite.
Of course, now that I know what I’m looking for, finding it again would be pretty easy.
Googling 301 redirect regex match gives me the right answer on the second hit. Go figure!
Unfortunately, that’s actually NOT where I found the info. That was a much more convoluted process. A shout out to WebWeaver for introducing me to RedirectMatch!





