Regex for beginners – tips for redirecting traffic

Revised Redirection Rule

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 When we archived that site, we moved everything to, 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$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, but after it added the tracking information, it looked like

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

3 thoughts on “Regex for beginners – tips for redirecting traffic”

  1. Evan Payne says:

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

  2. provdes says:

    Nicely explained. Helped me solve a regex issue for redirection that had been bothering me for day! Thanks :)

  3. crackboss says:

    oh no. pls help me. I applied the wide card that could take care of bulk actions and now my site cant open pages

Leave a Reply