Wordpress redirects

Posted on Fri 30 November 2012 in WebDev & Code

Moving from movabletype to wordpress has involved several post moving to new locations. The redirection plugin has made adding redirects easy but only once I figured out the correct regular expressions to use. Here's what I've had to use.

1) Old MT pages had .html on the permalink
Source: /(d*)/(d*)/(.*).html Target: /$1/$2/$3/

2) A much earlier MT site had pages in an 'archive' directory. They also had urls with underscores instead of hyphens the wordpress pages are using, instead of spaces generated from the page title. So some archive entries had one hypen, others had four. I didn't figure out a singe elegant regex and instead used five to do the job. Note, I couldn't use ".*" to match as "." would match the underscores too. The same applied to using the shortcut w as that also matches underscores.

No underscores
Source: /archives/(d*)/(d*)/([a-zA-Z0-9]*)/ Target: /$1/$2/$3/
One underscore
Source: /archives/(d*)/(d*)/([a-zA-Z0-9]*)_([a-zA-Z0-9]*)/ Target: /$1/$2/$3-$4/
Two underscores
Source: /archives/(d*)/(d*)/([a-zA-Z0-9]*)_([a-zA-Z0-9]*)_([a-zA-Z0-9]*)/ Target: /$1/$2/$3-$4-$5/
Three underscores
Source: /archives/(d*)/(d*)/([a-zA-Z0-9]*)_([a-zA-Z0-9]*)_([a-zA-Z0-9]*)_([a-zA-Z0-9]*)/ Target: /$1/$2/$3-$4-$5-$6/
Four underscores
Source: /archives/(d*)/(d*)/([a-zA-Z0-9]*)_([a-zA-Z0-9]*)_([a-zA-Z0-9]*)_([a-zA-Z0-9]*)_([a-zA-Z0-9]*)/ Target: /$1/$2/$3-$4-$5-$6-$7/