Revised Redirection Rule

Regex for beginners – tips for redirecting traffic

| 1 Comment

Regex is short for Regular Expression, which is something that crops up in many web languages, such as PHP and Javascript. It’s a way of matching a broad array of terms with a single rule.

For example, if you want to match all URLs that have a certain structure (perhaps they include a category base, which you have now removed, and you want to redirect all that traffic to the right location), instead of listing every single URL, you can provide one rule, which specifies a pattern that will trigger an action when it is found.

Basic usage

I’m still very much a beginner myself when it comes to Regex, but it has saved me so many hours of work, that I think it’s worthwhile sharing with you.

The most simple expression that has helped me is the (.*) expression, which means to look for any number of characters, or no characters at all. In plain language, it’s a wildcard, that matches anything, including if nothing is found.

So take a look at this example, using the Redirection plugin to create mass redirect rules. I recently completed a project for a client who was moving from a proprietary CMS to WordPress. The old CMS created URLs with the format http://www.hpcwire.com/hpcwire/articleurl.html. When we archived that site, we moved everything to http://archive.hpcwire.com, so any URLs taking the old format needed to be redirected to the same location on the archive site.

Since the original domain now hosted WordPress, note that we couldn’t do a blanket redirect for every single URL to the archive site, because there was now new content on it that needed to remain live.

So, I set up a single redirect rule that looked for all of the old content and sent it to the archive site, while ignoring all the new content. This is what the redirect rule looked like: URLs with the format /hpcwire/(.*).html were redirected to http://archive.hpcwire.com/hpcwire/$1.html.

Regex Redirection

Breaking it down, any URL on the main domain with the prefix /hpcwire/ and ending in .html, with anything in between those two things (denoted by the (.*)) would be redirected to the same location on the archive domain. Note that the $1 prints the first match that we looked for in our Regular Expression (we only had one in our rule, but you can have as many as you want).

This single redirection rule literally accounts for more than one million old articles from the old CMS, and redirects all that traffic appropriately, without any loss of performance on WordPress. In its first month, it has already redirected over 3 million requests. Certainly beats creating individual redirect rules for over a million URLs!

Another good use for Regex

I just came up with another good use for Regex in Redirection when I was checking over a newsletter which I had just sent out. MailChimp (at my request) was adding tracking information to every URL, so that I could look at my newsletter’s performance in Google Analytics. For example, one of the URLs I put in the newsletter was http://www.doitwithwp.com/elegant-themes-launches-divi-giveaway/, but after it added the tracking information, it looked like http://www.doitwithwp.com/elegant-themes-launches-divi-giveaway/?utm_source=Do+More+With+WordPress&utm_campaign=e942bcfe09-20131217&utm_medium=email&utm_term=0_82b86cdb26-e942bcfe09-196842993.

In my newsletter, I frequently use pretty redirect links that I created, so that I don’t have to remember URLs for all the services that I write about and recommend (some of which are affiliate links). The trouble is that my redirect rules were looking for exact matches. For example, my redirect rule to WP Engine looked like this:

Standard Redirect RuleHowever, because MailChimp was adding the tracking information to each link, the URL was no longer an exact match, so it was resulting in a 404 error on my site.

Thanks to Regex, there was a quick fix. Just add (.*) to the end of the source URL and turn it into a Regex redirect rule so that, so long as the URL started with http://www.doitwithwp.com/go/wpengine, it would redirect correctly to my intended destination.

Revised Redirection RuleOne word of warning for this use case: Let’s say that you have a Regex redirection rule that sends /go/host(.*) to an off-site URL, but you also have one for HostGator that take the form /go/hostgator(.*). If the first rule appears before the second rule, all traffic that you’re trying to send to HostGator will in fact go to the first URL instead, because /go/hostgator matches the first rule you set up (/go/host(.*)) and the rules are executed in order.

So, re-order your rules as needed, and craft them with this quirk in mind, using URLs that are fairly specific to avoid confusing multiple URLs and getting traffic sent to the wrong place.

Categories: Code & Snippets, The Basics | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Let a WordPress Expert help you

Do you want to try this, but feel like you need a helping hand, in case something goes wrong? My service, The WP Butler, gives you access to WordPress expertise whenever you need it. Better yet, I'll keep your site backed up, updated and secure, so that you don't have to worry about it. It's all part of the service. Use coupon DIWW and save 15% on all plans.

Visit The WP Butler

Author:

Dave has been tinkering with WordPress for many years, and he now shares his WordPress knowledge here on Do It WIth WordPress to help others realise its impressive power. He can also be hired to help with your WordPress needs. Dave, who is British, is married to his best friend, Marti, with whom he has a beautiful daughter, Ellie. When he's not dabbling with WordPress, he's probably eating Triscuits or hummus, watching an indie film or British TV show, spending time with friends or family, or exploring the world.

One Comment

  1. Useful tips, especially at the end there. Thanks Dave!

Leave a Reply