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:

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:

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:

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: Redirection Plugin, Regex, Regular Expressions, WordPress Plugins










Comments:
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
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!
@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 (-).
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!
@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!
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.
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/
@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?
@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.
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.
@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.
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
@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.
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!
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?
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
Hi Joe,
Try this:
Source URL: /reports(\d*)\.php
Target URL: /market-report/
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/
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
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.
/buyer-lis\([A-Za-z0-9-]*)\.php
doesn’t work either…
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.
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
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
Hi Joe,
Glad that you found the problem and glad that I could help! :)
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
Hi Joe, try this:
/neighborhood\.(.*)
It will match anything that is “/neighborhood.”
Thanks again Austin:
I tried /neighborhood\.(.*)
to redirect to
http://www.manausa.com/tallahassee-neighborhoods
But http://www.manausa.com/neighborhood-southwood-tallahassee.php gave me a 404 error instead of going to
http://www.manausa.com/tallahassee-neighborhoods
Joe
Hi Joe,
Oh, I made it so that it would match neighborhood.php.
To match neighborhood and then anything else, use this:
/neighborhood[\.\-]*(.*)
Hello,
I can I redirect:
http://www.bragaequipamentosdeconstrucao.com/contactos/
to:
http://www.bragaequipamentosdeconstrucao.com/contactos/bec/
in a way that this also works:
http://www.bragaequipamentosdeconstrucao.com/contactos/?lang=en
to:
http://www.bragaequipamentosdeconstrucao.com/contactos/bec/?lang=en
Thanks!
Trackbacks: