Why didn’t I think of this sooner!?

So I was giving myself a hard time by manually adding .htaccess match conditions whenever there was a 404 (file not found) error reported 1 which was related to the old permalink formats I used in my MovableType days.

I realized just how much of an idiot I was when I could’ve used regular expressions. As such, I was able to replace all of them with just these five lines of code:

RedirectMatch permanent ^/blog/archives/(.*)/([0-9]{4})_([0-9]{2})_([0-9]{2})(.*)\.php$ http://nargalzius.com/blog/archives/$2/$3/$4
RedirectMatch permanent ^/blog/archives/(.*)/index_([0-9]{4})\.php$ http://nargalzius.com/blog/archives/$2
RedirectMatch permanent ^/blog/archives/(.*)/index_([0-9]{4})_([0-9]{2})\.php$ http://nargalzius.com/blog/archives/$2/$3
RedirectMatch permanent ^/blog/archives/(.*)/index_([0-9]{4})_([0-9]{2})_([0-9]{2})\.php$ http://nargalzius.com/blog/archives/$2/$3/$4
RedirectMatch permanent ^/blog/archives/([a-z]{1,})($|/$) http://nargalzius.com/blog/archives/category/$1

Let’s break them down shall we?

Individual Permalinks

RedirectMatch permanent ^/blog/archives/(.*)/([0-9]{4})_([0-9]{2})_([0-9]{2})(.*)\.php$ http://nargalzius.com/blog/archives/$2/$3/$4

Basically is a match string for my old individual entry permalink which is in the format:

http://www.nargalzius.com/blog/archives/YYYY/MM/YYYY_MM_DD_HHMMXX.php Where XX is AM or PM

It utlimately checks the last part of the URL and checks if it’s got a “date grouping” (YYYY_MM_DD) which are a specific number of positive integers separated by underscores. It then disregards the existence AM/PM and makes sure it ends with .php. If it matches, then we’ve got ourselves an old permalink format, and it will proceed to do the voodoo it was intended to do.

The only change with regards to the redirect behaviour of this “new” way is that since there’s no way of “factoring” the AM/PM to get to an individual entry… it’s going to redirect to the date-based archive.

For example, with the old manual method, clicking on this link: http://www.nargalzius.com/blog/archives/2008/07/2008_07_31_1506PM.php would’ve taken you directly to this page. That obviously isn’t the case anymore. But you can still see the same post registered in the date-based archive. So overall, even with the new way, it should show enough information for the redirected user to click on which “post” he/she probably was looking for.

The best thing about this is that it’s kinda “forward thinking” as far as anticipating 404s. I didn’t have time to sit down and map out all my entries to the new URLs, so I would just wait until I got a 404 report that wasn’t from a search engine spider/bot. At least with this approach, it covers everything – pretty effectively at that.

Date-based Archives

We then got provisions for the date index pages.

RedirectMatch permanent ^/blog/archives/(.*)/index_([0-9]{4})\.php$ http://nargalzius.com/blog/archives/$2
RedirectMatch permanent ^/blog/archives/(.*)/index_([0-9]{4})_([0-9]{2})\.php$ http://nargalzius.com/blog/archives/$2/$3
RedirectMatch permanent ^/blog/archives/(.*)/index_([0-9]{4})_([0-9]{2})_([0-9]{2})\.php$ http://nargalzius.com/blog/archives/$2/$3/$4

The old system referenced flat files as far as general date based archives went. They were similar to the individual entries – only they didn’t have any time information… and they were all preceded by index_

The three lines simply match the [three] possible cases in which the user might’ve stumbled upon a date-based archive in the old permalink format; namely a yearly, monthly, or daily archive. Then it would and redirect accordingly.

Category Archives

As for the code below:

RedirectMatch permanent ^/blog/archives/([a-z]{1,})($|/$) http://nargalzius.com/blog/archives/category/$1

The old permalink sructure for category pages was /blog/archives/category_name. For some reason, the WP people decided to make it difficult to retain such a structure without horrible side-effects. So you essentially have to put them in a child directory for it to work properly (e.g. /blog/archives/category/category_name).

The code we just mentioned matches the old permalink, by checking if a single word comprised of letters only finishes the URL – which basically indicates that it was probably a category archive. Then redirects it accordingly.

Notes

Notes
1 which was related to the old permalink formats I used in my MovableType days.

Have a say

This site uses Akismet to reduce spam. Learn how your comment data is processed.