Redirection Plugin and Regexes

posted on February 1, 2009

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!

Tags: , , ,

Like this post? Someone else might, too.

  • RSS
  • Reddit
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Technorati
  • Twitter
  • Digg

50 Responses to “Redirection Plugin and Regexes”

Comments:

jez says:

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

Todd says:

Anyone know how to write an expression to change:

http://www.homeconstructionimprovement.com/2007/01/engineer-architect-builder-wing-it.html/feed

To

http://www.homeconstructionimprovement.com/engineer-architect-builder-wing-it/feed

More specifically how to remove the year, month and .html from the post name.

Thanks in advance!

Austin says:

@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 (-).

Eytan says:

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!

Austin says:

@Eytan

Try this:

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

You don’t need to include index.php in the redirection even if it appears in all of your URLs. It’s unnecessary since it is matched by the (.*) regex. Also, make sure that you include http:// in the target URL. Otherwise you would end up with a URL such as http://site.com/newsite.com/index.php/post-1.

I hope this helps!

JohnyB says:

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.

Austin says:

Hi JohnyB,

You can’t do this with the redirection plugin. You could do it with ModRewrite, but it’s probably easier to just use a plugin.

Here’s a few that links to check out:

http://www.hongkiat.com/blog/wordpress-plugin-create-seo-friendly-search-url/
http://www.searchenginejournal.com/seo-friendly-url-structure/4556/

bill bennett says:

@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?

Austin says:

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

bill bennett says:

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.

Austin says:

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

Paul says:

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

Austin says:

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

Ivo Schmid says:

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!

Austin says:

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?

Joe Manausa says:

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

Austin says:

Hi Joe,

Try this:

Source URL: /reports(\d*)\.php
Target URL: /market-report/

Joe Manausa says:

Hey Austin,

Thanks for the reply. But it is not working.

Here is the real world address:

http://www.manausa.com/buyer-lispendens041008.php

I want to redirect all of the “lispendens*.php”

to

http://www.manausa.com/foreclosures

So following your reply (I think), I did this redirect:

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

to

http://www.manausa.com/foreclosures/

Joe Manausa says:

Austin,

Couldn’t I do this as just a character redirect, something like (but obviously not):

/buyer-lisp(*)

to

http://www.manausa.com/foreclosures/

Because after the “-lisp” they are all needing to go to that new redirect.

Thank you.

Joe

Joe Manausa says:

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.

Joe Manausa says:

/buyer-lis\([A-Za-z0-9-]*)\.php

doesn’t work either…

Austin says:

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.

Joe Manausa says:

Thank you Austin,

I tried and it failed again…

I’ve made other Regex solutions work, this one will save me about 30 redirects.

Screen capture of command.

http://www.manausa.com/wp-content/uploads/2010/01/BuyerLP-Regex.JPG

What could I be doing wrong?

Joe

Joe Manausa says:

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

Austin says:

Hi Joe,

Glad that you found the problem and glad that I could help! :)

Joe Manausa says:

Austin,

If I want to redirect

http://www.manausa.com/neighborhood.php?p_neighborhood=867

can it be done to send anything that starts with

http://www.manausa.com/neighborhood

to the same page (i.e.)

http://www.manausa.com/neighborhood/

Joe

Austin says:

Hi Joe, try this:

/neighborhood\.(.*)

It will match anything that is “/neighborhood.”

Austin says:

Hi Joe,

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

To match neighborhood and then anything else, use this:

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

Calebe Aires says:

Hi

I need an regex that do this:

http://exemple.com/post-name/favicon.ico

to:

http://exemple.com/post-name/

(without favicon.ico)

The plugins is detecting the url “http://exemple.com/post-name/favicon.ico” as an 404 error

Very thanks

Austin says:

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.

Nancy says:

Hi Austin,

I need a regex that will change…

http://www.sitename.com/blogname/category/postname

to

http://www.sitename.com/blogname/postname/

I almost have this, but do not know the regex for “category”

Thanks in advance.

Austin says:

Hi Nancy,

Try this:

Source URL: /([A-Za-z0-9-]*)/([A-Za-z0-9-]*)(/)?
Target URL: /$2/

Thanks for commenting!

rob furness says:

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

Tim says:

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

Robert says:

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!

Nipun says:

Thanks a lot for this awesome awesome article. This has helped me a lot. wonderful information.

Robert says:

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?

Tim says:

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!

Chris says:

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

shob says:

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

Dan says:

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!

rajkumar says:

hi,
i have a problem please any one suggest

source : http://mysite.com/gallary-details?gallery_id=30

Target : http://mysite.com/gallary-details/30

for all the number like 31,32 i want in regular expression

Thanks in advance

DikMa says:

use :

/(\d*)/(\d*)/(\d*)/(.*)/ => http://domain.com/2009/09/09/post/

target :

/$4/ => http://domain.com/post/

great!!

ebele says:

I need help.

I want to redirect many urls like the one below

http://oilandgasbrief.com/oil-gas-companies-nigeria-oildata-wireline-services/722/

to something like this

http://oilandgasbrief.com/oil-and-gas-careers/oil-and-gas-jobs/oil-gas-companies-nigeria-oildata-wireline-services/722/

using the redirection plugin. I have tried with no success. Please help.

Thanks.

Khalid says:

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.

Austin says:

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

Trackbacks:

Leave a Reply

Subscribe to gulati.info

Subscribe via RSS (?) Subscribe to our RSS feed Subscribe via Email
Follow me on Twitter Follow me on Twitter