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. hi there,
    excellent article on ub’s plugin!
    i wonder if i can use this to redirect posts to subdomains, too and if so how.
    cheers, jez

  2. @Todd

    Maybe this:

    Source URL: /(\d*)/(\d*)/([A-Za-z0-9-]*).html/feed
    Target URL: /$3/feed

    That should work, just make sure that your permalinks only have letters and hyphens (-).

  3. Hey All!

    Im trying to redirect site.com/post-1

    to

    newsite.com/post-1

    Basically making sure that all of my old posts get redirected to the new site.

    I have tried this: /index.php/(.*) => newsite.com/www.goingwimax.com/index.php/$1

    base on another blog but no luck. Any ideas? Im desperate!

  4. Hallo,
    I need that my search results look like http://www.domenname/search-results-for-keyword.html.
    WordPress is installed in root directory.
    I tried with /?s=(.*) => /$1\.html to create redirection from http://www.domenname/?s=keyword to http://www.domenname/keyword.html (URL look like http://www.domenname/?/keyword.html and I think that pages which displaying are in index.php and not search.php).
    I tried to manually modify htaccess file and I can’t make it work.
    Also, I can’t solve how to change “+” in to “-” in URL of search results?
    Please somebody help me. Thank you.

  5. @Austin

    I’ve tried using your suggestion for Eytan – September 29 (not at my site, but one owned by my wife).

    Source URL: /(.*)
    Target URL: http://newsite.com/$1

    This gives me an error, sending the server into a horrible infinite loop that can’t be resolved.

    Any other ideas?

  6. @bill

    This would only work if you have two separate blogs. You would need to have a blog installed on http://oldsite.com/ with that redirection created, and then a separate blog at http://newsite.com/ without the redirect.

    AFAIK, the redirection plugin doesn’t allow you to have a domain in the source URL, so we couldn’t do that.

    What exactly did you do? You changed the domain of the blog? If you have two separate blogs, you want to use the old one. I’m pretty sure that if you have one blog with two domains pointing to it, WordPress will automatically redirect to the domain that you have set under ‘Settings’ > ‘General’ in the admin panel.

  7. Hi Austin

    Yes, it’s one WordPress site with two domain names. We had to change the name for business reasons. There are many external links to the old site name – when traffic comes from one of those links they see:

    oldsitename.co.nz/story1

    And if they then hit a link they are directed to:

    newsitename.co.nz/story2

    But it would be best if incoming traffic was automatically redirected to the new site name.

    I’ve done this before using .htaccess, but for some reason the code that works on my other site doesn’t work the same magic this time around.

  8. @bill

    Hm… WordPress doesn’t do these automatically? I always thought it did.

    Anyway, post what you have for the .htaccess file and I’ll take a look to see why it’s not working. It’s going to have to be a conditional statement, so we can’t do it with the redirection plugin.

  9. Austin, a client has a site – site.com/postname
    but since he moved it to a different hosting company he needs index.php in his postnames. So now he has
    site.com/index.php/postname, what would be the codes to make Redirection work for his senario?

    Thanks
    Paul

  10. @Paul

    I’m pretty sure that this would require a conditional statement. I’m not a master of mod_rewrite, but I’ll find a solution for you soon.

  11. Hello @ all!

    Maybe my problem is much easyier to solve, than I imagine…
    But, I do not see a way to do it right.

    I have alot of requests, trying to insert a comment via the WP bridge to Gallery 2 (Menalto) and, as this feature is not enabled, it ends up in a 404 error.

    Example from my logs:
    http://www.danivo.net/wp/main.php?g2_view=comment.AddComment&g2_itemId=7880

    http://www.danivo.net/wp/main.php?g2_view=comment.AddComment&g2_itemId=4985

    …and so on… => Gives 404

    As you see, the URL is consistent, only the “itemId=” number changes.

    How could I cover this matter in Redirect, sending those queries to an info page, informing the users about the fact, that this cannot been done?

    Any help is highly appreciated!

  12. Hi Ivo,

    Here’s a regex that would match that URL:

    /wp/main\.php\?g2_view\=comment\.AddComment&g2_itemId\=(\d*)

    And then you could just redirect that to a WordPress page that explains that comments cannot be posted.

    Did I understand your question correctly?

  13. How do you redirect many pages to one new one. For example,

    http:mysite.com/reports080707.php
    http:mysite.com/reports080714.php
    http:mysite.com/reports080721.php
    http:mysite.com/reports080731.php

    all to go to http:mysite.com/market-report/

    Thanks,

    Joe

  14. ahhhhh. Very frustrating. I think I’ve tried everything but the right answer.

    Even tried these:

    /buyer-lis(.*)\.php

    and

    /buyer-lis\(.*)\.php

    Anybody have reason why this doesn’t work?

    Thanks.

  15. Hi Joe,

    That’s weird, this works for me:

    /buyer-lispendens(\d*)\.php

    (Try it out, it should redirect here: http://gulati.info/buyer-lispendens041008.php )

    I would first suggest that you make sure that the ‘Regex’ box is checked. Then, I would make sure that you refresh the page after you make changes and then make sure that everything is correct. Sometimes if you make changes the AJAX fails and the changes aren’t actually made.

  16. Austin,

    I found the problem…finally.

    I had an old failed version that I had not deleted. Once deleted, this solution works perfectly.

    Thank you again for your help.

    Joe

  17. Hi Joe,

    Oh, I made it so that it would match neighborhood.php.

    To match neighborhood and then anything else, use this:

    /neighborhood[\.\-]*(.*)

  18. Hi Calebe,

    The regular expression would be:

    Source URL: /([A-Za-z0-9-]*)/favicon.ico
    Target URL: /$1/

    Is that really what you want to do, though? You might want to redirect it to the location of your actual favicon.ico so that the favicon appears to users.

  19. Hi,
    Great advice pages on redirects, though i seem to be having a problem no one as mentioned.

    Old blog had permalings set to include month and year, now to match best seo practice I wish to strip out the times from the url leaving just the post name.

    Changed permalinks ok, and created a rule

    /blog/(\d*)/(\d*)/([A-Za-z0-9-]*)

    this works nfine and ensure that google links are redirected.

    But:

    this rule seems to also catch the site links for going to the archive lists eg archives which have the same format except dont have a post-name after the /.

    is there a way to specify expressions that apply if there is a post_name at the end but dont apply if its less specific.

    of course maybe I’m going nuts and there is another gremlin at play, but if i turn redirects off I can reach the archive fine.

    Any help appreciated.

    Thanks

  20. Hello Austin, thanks for the very helpful post and information here.

    If you don’t mind, I’d appreciate some pointers on a redirect issue I’m having. I’m switching blogging services (to WordPress) and the old URLs we have follow this pattern:

    domain.net/YEAR/MONTH/DAY/title-of-post-that-is.html

    We’re hoping to automatically redirect all the old posts to the new format, which is:

    domain.net/YEAR/MONTH/DAY/title-of-post-that-is-not-truncated.html

    Does that make sense? Basically I’m wondering if it’s possible for to use the first few words of the title of post to automatically redirect to the corresponding new address. Any help would be much appreciated.

    Thanks,
    Tim

  21. Hi Austin,

    First I want to say thank you for this awesome article, and for taking the time to help so many people with this problem. I have been fearing changing my site because of this exact problem, and to find it is so easy is amazing!

    If I could ask to help my own little problem, i would be extremely grateful.

    my current format is:

    http://mysite.com/article.php?article_id=9033 – its a non-wordpress site I am moving to wordpress.

    I would like to change it to:

    http://mysite.com/postname/

    right now I have redirection set as follows, but I its not working correctly.

    source url: /article.php?article_id=(\d*)

    target url: /$4/

    Any help you could give would be awesome! thanks in advance!

  22. I realized after a lot of reading that this just won’t work :(

    But it can work with the permalink redirect plugin (kinda)

    I can redirect

    /article_id=post_id

    to

    /postname/

    BUT

    It doesn’t seem to work if I try to redirect

    /article.php?article_id=post_id

    but I was thinking I could use both plugins and do 2 redirects. would that be possible?

    redirect #1: /article.php?article_id=post_id to /article_id=post_id

    redirect #2: /article_id=post_id to /postname/ through the other plugin.

    do you think that would work?

  23. Hi – wondering if someone can help with,

    redirecting posts prefixed by /index.php/ (eg. /index.php/(.*)

    to new permalink structure,

    /%year%/%monthnum%/%postname%/

    I am not sure what the regex is for the destination.

    I am really stuck – help much appreciated!

  24. Anyone know how to do a case insensitive match. I need to redirect /inspections to http… and would like to have the /inspections case insensitive.

    It works if I just do the first letter…[iI]nspections but would like to do something like
    /(inspections/i)

    The /i for case insensitive does not recognize the pattern.

    Thanks,
    Chris

  25. I had been using following permalink.
    mysite.com/%postid%.html

    now i am using this permalink
    /%year%/%monthnum%/%postname%/

    What regex should i use?
    Thanks
    Shob

  26. How do I remove the year from my WordPress URL by using a mod_rewrite?

    Will this adversely affect SEO in any way?

    Thanks in advance!

  27. Hi Austin,

    This may be a stupid question, but I am having a lot of trouble configuring the plugin to do what I need it to do.

    I had my blog originally at:

    http://www.emagine-group.com/behindthechairmansdoor/

    I have since moved it to a seperate domain called btchd.com. The problem that I am having is that I am not able to get your redirection plugin code right so that it will automatically redirect to the new blog.

    The original links were setup like this:

    http://www.emagine-group.com/behindthechairmansdoor/2007/07/04/lal-masjid-in-pictures/

    I would like to redirect it to:

    http://www.btchd.com/2007/07/04/lal-masjid-in-pictures/

    What I need is a general code that will allow me to redirect all the URLs from the old blog to the new one without having to do a lot of coding.

    Please help.

  28. @rob

    Hm, I’m not really following. Could you give me an example?

    @Tim

    This isn’t something that the redirection plugin could do unless you put in each URL individually was a redirection.

    @Robert

    The title of the article doesn’t appear in the source URL, so this is not something that the redirection plugin could do. When you use “$4″ in the target URL, it refers to the fourth block in the source URL. There is only one “block” (which are defined by parenthesizes), so only “$1″ would work and that would refer to the contents of “\d*”.

    Oh, just saw your second post. The problem with that would be that the post_ids from your old blogging software to the new one wouldn’t be the same.

    @Tim (second one)

    What is the permalink structure that you used that included index.php? Once I know that I can construct the regular expressions.

    @Chris

    Hm, there isn’t an easy way to do that as far as I know…

    @Fatih

    Try this:

    Source URL: /(\d*)/(\d*)/(\d*)/([A-Za-z9-0-]*)/?referer=.*
    Target URL: /$1/$2/$3/$4/

    @shob

    This isn’t something you can do with the redirection plugin… WordPress should automatically redirect something like that though.

    @Dan

    It shouldn’t affect SEO. What is your permalink structure?

    @Dunia

    Try this:

    Source URL: /(\d*)/(\d*)/([A-Za-z0-9-])*.html
    Target URL: /$1/$2/$3#comments

    @rajkumar

    Try this:

    Source URL: /gallary-details?gallery_id=(\d*)
    Target URL: /gallary-details/(\d*)

    @ebele

    The redirection plugin can’t do that since the categories in the target URL are not found in the source URL. If you change your permalink structure, WordPress should do that automatically though.

    @Khalid

    Is your blog still set up there? The redirection plugin should be set up on the old blog, not the new one.

    @Everyone

    Thanks for your comments and questions! If you still have any more questions, please feel free to leave another comment.

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>