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:
If this article has been helpful, please consider linking to it or commenting.
