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/

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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

  2. This works for me directly through htaccess.

    For Day and Name (/%year%/%monthnum%/%day%/%postname%/)

    RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ /$4

    For Month and Name (/%year%/%monthnum%/%postname%/)

    RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(.*)$ /$3

    For Numeric (/archives/%post_id%)

    RedirectMatch 301 ^/archives/(d+)$ /?p=$1

    Note: Use it after https redirect