Search code examples
regexwordpresspermalinks

Permalink Redirection and Regular Expressions in WordPress


I've got a pretty basic RegEx question but it's pressing enough that I don't have time to run down a tutorial on RegEx in order to answer it myself.

In short, I need: /2009/01/28/post-name/

redirected to /post-name/

As well as /post-name/author-name/

redirected to /post-name/

I promise to go take a class in RegEx before the month is out. Deal?

PS. Yes, I know that the Wordpress Redirection plugin will automate .htaccess but it won't write RegEx for me.


Solved using the Redirection plugin.

Source URL: /(\d*)/(\d*)/(\d*)/([A-Za-z0-9-]*) Target URL: /$4


Solution

  • As this may be found by someone in the future, I'm going to answer in as much detail as possible. I actually posted a blog on the subject here where I could discuss it.

    The simplest solution uses very basic regex in concert with the Redirection plugin for WordPress. In this instance, you can use simple structures:

    • (\d*) for a numeric string
    • ([A-Za-z0-9-]*) for an alphanumeric string
    • $# (in my case $4) to determine which of the outputs to keep
    • a / for the directory seperator

    So in the case of /yyyy/mm/dd/post-name we use:

    • Source URL: /(\d*)/(\d*)/(\d*)/([A-Za-z0-9-]*)
    • Target URL: /$4

    in order to output /post-name as a 301.

    Don't forget to check the regex box!

    gui interface for the redirect