Rewriting/redirecting old Wordpress permalinks to new


While setting a new permalinks layout with Wordpress is a trivial task, redirecting users from the old url model to the new one can become a tricky. I wanted to switch from this layout:

to that one:

I didn’t want to loose all my incoming links. For sure I wanted to use some kind of redirect. One of my Facebook friends recommended a plugin like this. There are three pretty similar plugins of that kind in the WordPress repository. Looking at the code I found out they simple catch a 404, look in the database if there is a matching title and then return the found item. It leaves the question open what these plugins do when the title is not unique. I am pretty sure they are returning the first one and simply ignore the later. However, this would have been the dirty solution.

Another colleague I know from very old times sent me a regex hack, which was not complete, but so good I could extend it to fit at least. Finally my regex on a standard 1und1 host looks like this:

RedirectMatch ^/([0-9]{1,4})/([0-9]{1,2})/([0-9]{1,2})/(.*)\.html$$4-$3$2$1.html [R]

Pretty simple, once you have learned it. ^ is the beginning of the input string, $ marks the end. Then you grab the numbers between the slashes and store it in temporary variables. In my case its filled with the date. After all I create the new url.

I should have been able to add a status to the RedirectMatch according to the specs . But this did not work out for me - don’t know why.

Tags: #Open Source #PHP #Wordpress