Redirection Plugin – Regular Expression Examples from a Movable Type to WordPress Migration

Problem:

Within a few days of migrating a Movable Type blog to WordPress we had thousands of 404 errors with users still hitting our old MT Search Engine. The Redirection plugin logged all of them, and they all seemed to follow 4 main URL formats:

  • /cgi-bin/mt/mt-search.cgi?tag=shoes&Template=1&IncludeBlogs=1
  • /cgi-bin/mt/mt-search.cgi?blog_id=1&tag=kareem&limit=20&IncludeBlogs=1
  • /cgi-bin/mt/mt-search.cgi?blog_id=1&tag=la&limit=50
  • /cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&tag=gym&limit=20

Goal:

The goal is to “capture” what the user is trying to search for, forward that to the new wordpress search engine, which will then provide the results of the search on the new blog.

Solution:

We created some regular expression redirects using the Redirection Plugin. Basically,  you format the “Source URL” with (.*) where you want to “Capture”. Then in the Target URL you insert your previously captured data using $1, $2, or $3.

$1 : Inserts the first set of data captured by the regex.

The question mark has a special meaning within regular expressions, it must be “escaped” so when the regular expression is running it treats the ? as a normal character. You do this by using the backslash \. There are 11 characters that have special meaning: [ \ ^ $ . | ? * + ( ) These are often called metacharacters, read more about regular expressions here.

Examples:

These are the Rules I used for each of the 4 formats mentioned earlier:

404 Error Sample: /cgi-bin/mt/mt-search.cgi?tag=shoes&Template=1&IncludeBlogs=1
Source URL: /cgi-bin/mt/mt-search.cgi\?tag=(.*)&Template=(.*)&IncludeBlogs=(.*)
Target URL: /?s=$1
Explanation:
The “tag” is the only thing we really need to capture. We capture the Template and IncludeBlogs so the redirect works no matter what those variables are set to. The $1 is used because its the first set of data we capture.

404 Error Sample: /cgi-bin/mt/mt-search.cgi?blog_id=1&tag=kareem&limit=20&IncludeBlogs=1
Source URL: /cgi-bin/mt/mt-search.cgi\?blog_id=(.*)&tag=(.*)&limit=(.*)&IncludeBlogs=(.*)
Target URL: /?s=$2
Explanation:
The “tag” again is the only thing we really need to capture, and its the second set of data in the original URL. Hence the $2. We again capture the Blog ID, Limit Results,  and IncludeBlogs so the redirect works no matter what those variables are set to.

404 Error Sample: /cgi-bin/mt/mt-search.cgi?blog_id=1&tag=la&limit=50
Source URL: /cgi-bin/mt/mt-search.cgi\?blog_id=(.*)&tag=(.*)&limit=(.*)
Target URL: /?s=$2

404 Error Sample: /cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&tag=gym&limit=20
Source URL: /cgi-bin/mt/mt-search.cgi\?IncludeBlogs=(.*)&tag=(.*)&limit=(.*)
Target URL: /?s=$2

Screenshot:

Subscribe

Subscribe to our e-mail newsletter to receive updates.

4 Responses to “Redirection Plugin – Regular Expression Examples from a Movable Type to WordPress Migration”

  1. Dave October 17, 2011 at 10:01 am #

    Please help… I'm trying to figure out how to redirect:

    /comment-subscriptions?srp=3672&sra=s

    where the only changing variable is the set of numbers after the = and before the &

    I have set up the redirect to find this:

    /comment-subscriptions?srp=(.*)&sra=s

    (with Regex on)

    But it's not working so far. Any tips? Many thanks in advance!

    • iainlbc October 17, 2011 at 10:09 am #

      Hi Dave,

      Sorry to hear that…Your regex is correct.

      Can you try creating a sort of control group to narrow down the issue? You could try create a new page "test" then try doing a redirect of /test to another page just to make sure the plugin and underlying mod_rewrite is working.

      You could also try doing the full url of the site (http://yoursite.com/comment-subscriptions?srp=(.*)&sra=s) ?

      Are you certain apache mod_rewrite is enabled on your host? Do you have control of the apache config/vhost?

      Cheers!

  2. Dave October 17, 2011 at 10:11 am #

    Aha, I figured it out. Like YOU said (I guess I just glossed over it…), the question mark has to be treated differently if you want it read literally. Anyway, thanks for the tutorial. Very helpful.

    • iainlbc October 17, 2011 at 10:13 am #

      Oops…didn't catch that either ;) Glad its working!

Leave a Reply