Redirection Plugin and Regexes

I have recently getting a few questions on using Regexes (Regular Expressions) with the Redirection Plugin for WordPress. After some testing and asking for more information, I have figured out the most affective way to use the regexes with the WordPress plugin.

Simplified Regular Expression

The first option that you have is to use a simplified regular expression. You don’t have to know anything about regular expressions to use this. All you need to do is put an asterisk (*) at the end of your Source URL. Heres an example:

You can use a simple regular expression with the redirection plugin for WordPress.

This is redirect any URL that contains /blog/ at the start. The easiest way to explain the redirects is through examples:

Start URL End URL
gulati.info/blog/2009/01/using-wp-super-cache/ gulati.info/2009/01/using-wp-super-cache/
The wildcard text is automatically appended after the Source URL specified. Since 2009/01/using-wp-super-cache/ is after /blog/, it is attached to / to give the URL /2009/01/using-wp-super-cache/
gulati.info/blog/ gulati.info
Since there is nothing after /blog/, it just redirects to the Source URL /.
gulati.info/blog/blog/ gulati.info/blog/, which then redirects to gulati.info/

This is a fairly simple redirect that requires no regular expression knowledge. This is great if you moved a blog from http://domain.com/blog/ to http://domain.com/ or made a similar change.

Advanced Regular Expressions

You can also use full regular expressions with the redirection plugin. In my opinion, this is rarely necessary, but the redirection plugin still allows it. Here is an example setup:

You can use full regular expressions with the WordPress redirection plugin.

This is a great setup if you have just switched your permalink settings from Day and name to Month and name. If you don’t understand regular expressions, heres a quick rundown:

This diagram illustrates a regular expression that can be used with the redirection plugin.

Again, here’s a table of examples:

Start URL End URL
gulati.info/2009/01/27/using-wp-super-cache/ gulati.info/2009/01/using-wp-super-cache/
gulati.info/2009/01/using-wp-super-cache/ gulati.info/2009/01/using-wp-super-cache/
(it doesn’t match, so no affect)
gulati.info/32523/234/2342/random-numbers/ gulati.info/32523/234/random-numbers/

I hope that this helped! Regular expressions themselves are a hard concept to grasp (I suck at them myself), but using them with the redirection plugin is fairly easy. If you have a question, please leave a comment!

76 Responses

  1. I would like to start off by stating, thank you for supplying me with the information I’ve been searching for. I’ve been surfing the net for three hours searching for it and would have given my right arm if I would have found your site sooner. Not only did I find what I was looking for, but found answers to questions I never thought to ask. Thank you for such a wonderful web-site!

  2. @Eila

    No problem, glad that I could help.

    @Katie

    That falls under the category of what WordPress should automatically redirect. That’s not something that you can do with the redirection plugin – it only redirects based on information that is in the source URL. Your source URL (?p=123) doesn’t contain the information that’s in the target URL (category name and post name), so that redirect can’t be done. WordPress should redirect automatically (for example, if you go to http://gulati.info/?p=625 it automatically redirects to this post. Hope that helps!

  3. Hi Austin, thanks for this post (and explanations afterwords!).

    I have the same problem as Rob Furnass, but I’ll try to explain it a bit clearer.

    I have changed from /year/month/postname -> /postname.
    with great help from your instructions I’ve created a redirect saying:
    Source URL: /(\d*)/(\d*)/([a-zA-Z0-9]*)/
    Target URL: /$3
    This works like a charm!

    But, I have previously used wordpress archive widget that simply gives you a shortlist with all posts from a specific month and those pages have a URL like: /year/month/ and the redirection above catch this as well.

    Do you know if it’s possible to create the redirection so it only catches /year/month/postname and ignore /year/month/ or some other workaround? I’ve been experimenting around without any luck so far.

    Ideas would be very welcome!
    /Osky

  4. Hey
    Really want to thank you for the post! I have tried to figure out the redirection for a while now… and after your post several times i think i got it:) Thank you again!

  5. Yikes!

    I originally set my site up as http://www.mydomain.com/blog/ using wordpress as an add on to my static site for blog. Since then I have moved all my static pages into wordpress and have been using a simple index.html entry page leading into mydomain.com/blog/

    I decided it was time to get rid of the /blog/ and have mydomain.com pull up my WP pages and posts etc.

    So, I followed the instructions at http://askwpgirl.com/how-do-i-move-wordpress-from-a-subdirectory-to-the-root-directory/ and everything went well, my pages and posts work etc. I also confirmed this was a correct method on the codex page (albeit the instructions there were for the opposite direction). Cool so far.

    I then attempted to use redirection to strip /blog/ out by using the regular expression /blog/* to target / as suggested here.

    This leads to a new problem, it redirected everything (awesome) including mydomain.com/blog/wp-admin/ so now I can’t log in.. (not so cool per se!)

    So, luckily I had another tab open in my browser and I was able to delete that regex and can get into my blog but I am still stymied on how best to redirect everything but my wp-admin. Does this make sense? Anyone can help? Please?

  6. Hi,

    Thanks for this post. I’m moving my new web site to wordpress. This plugin works great with for my old pattern for example:

    my old adresses: http://www.mysite.com/index.php?module=contact
    i redirect it: http://www.mysite.com/contact/

    but i can’t redirect that adresses with url parameters

    http://www.mysite.com/index.php?module=contact&var1=string&var2=string

    to

    http://www.mysite.com/feedback/?var1=string&var2=string

    i tried regex but i didn’t redirect with parameters

    Regards,

  7. reply to Hannes,

    You are missing the ‘/redirect’ string.

    Your source URL requires to reference everything after your domain like this;

    Source URL:
    /redirect?url=(.*)

    Target URL:
    $1

    Kind regards
    Franky

    PS. anyone know how to make a regex argument case insensitive?

  8. Hello,

    Do you what method this Redirect Plugin uses to redirect? I’m looking into preserving backlinks and pagerank for an old-to-new-site-project and was wondering how this plugin does the trick.

    Thanks!

  9. i tried the first example above but it isn’t working for some reason. instead of ‘blog’ i had ‘Meta’

    What i’ve done is i have two separate installs on the same database (one is ‘blog’, one is ‘Meta’); i’ve imported the ‘Meta’ posts to ‘blog’ and want to redirect there. Also, i have ‘blog’ set up so that it instead of http://www.archetypalimages.com/blog/content
    it is directed to show
    http://www.archetypalimages.com/content

    Not sure if this is part of my problem or what. I’ve tried some other combinations to see if i can get it to work, but nothing is. I haven’t deleted the ‘Meta’ install yet, in case that makes a difference.

    Hope you can help! Thanks!!!

  10. Hi, I’m trying to do a redirect
    from /articolo.cfm?id_articolo=21788 to /21788
    the “rule” with regexp (if i’ve read the documentation correctly) is
    /articolo.cfm?id_articolo=(.*)
    /$1
    but i’ve always a 404 error
    Have you got any idea?

    Thanks Riccardo

    • Hi Ricardo,

      Try this as a source URL:

      /articolo.cfm?id_articolo=([0-9]*)

      Make sure that the Regular Expression box is checked.

      Thanks! – Austin

  11. Hi Austin,

    Thanks for your tutorial! I am very new to Regex and I was able to successfully redirect my /year/month/day/posttitle links to /posttitle

    However, I want my:
    http://site.com/admin/publish?id=101 to redirect to http://site.com/?p=146
    and
    http://site.com/admin/publish?slug=posttitle to redirect to redirect to http://site.com/posttitle

    Someone on Webmasters.StackExchange.com already told me how to redirect site.com/admin/publish?id=101 to site.com/?p=101 with an Apache rewrite rule (RewriteRule ^admin/publish?id=(.*) /?p=$1) but I would like the redirection plugin to manage all my redirects.

    Any help or pointers would be greatly appreciated. :(

    Thanks,
    Jane

    • Hi Jane,

      Try this:

      Source URL: /admin/publish?id=(.*)
      Target URL: /?p=$1

      Source URL: /admin/publish?slug=(.*)
      Target URL: /$1

  12. Thank you for this post. Question about reg exp. I am using ([a-z]*) as part of my reg expression. How do I change it so that a hyphen is also matched; in other words, how do I tell it to match any characters that are a hyphen or between a and z. Thanks

  13. Austin, thanks for the tutorial. I am having a heck of a time trying to figure his one out, maybe you can help. I am trying to use the plugin to redirect a bunch of old user account urls to /

    /~USERNAME

    Many have subdirectories under that one:

    /~USERNAME/SUB/SUB

    I have tried using the regex functionality. The tilda seems to cause some escaping in the plugin, so I tried escaping them…with no luck. My last attempt was:

    /\~[a-zA-Z].*

    but, no love.

    Thanks,

    Jason

  14. Hi, thanks for your post & help here.
    I changed the organization of my blog and am testing the plugin, with great results so far…
    e.g. this works fine: /(\d*)/(\d*)/(.*) => $3
    But i can’t have this 2nd one to work.
    /Index.php?tag=(.*) => /tag/$1 (e.g. bad that should be redirected to correct )
    It’s not that the expression gives strange or bad results, it’s not fired at all… did I forget something? the regex checkbox is checked…
    Nothing happens, and I’m wondering why…

    sincerely
    DJM

  15. OK, just found 2 things I did wrong:
    * escape the question mark => \?
    * order regex expressions in the dashboard from the most specific to the less one.
    Pb solved!
    Sincerely
    DJM

  16. Is it possible to use Redirection to update an entire domain? I had a blog at initialdomain.com – now I’ve switched it to secondomain.com. The old domain is still pointing to the seconddomain but when the visits come in I want them to be redirected so eventually everything is 301ed and seconddomain.com is what shows up in all of the search engines. Basically, I just migrated the blog from one domain to another.

    • Hi Geoff,

      Yes, this is possible. Try something like this:

      Source URL: *
      Target URL: http://newdomain.com/

      To get this to work, though, you will need to have two totally separate sets of WordPress files. The old one will have to be running the redirection plugin with the above redirection and the new one will be totally independent from that. I’m pretty sure that WordPress handles things like this on its own. What happens when you access a post via the old domain?

      Hopefully this helps. Sorry about the delay, have been neglecting my blog somewhat.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>