Starting and ending your redirect URL with regular expressions
The first crucial components of your regular expression redirect are the “start” and “end” characters. This allows you to dictate the beginning and end of the expression to match, and prevents a lot of accidental redirect loops when you use it correctly. Here’s an example:
And here’s what this looks like in the plugin:
Note that the “Regular expression” box must be checked for the plugin to respect your regular expressions.
This expression will only apply to the page http://yourdomain.com/old-page/, in exactly that format. If you didn’t include your start (^) and end ($) characters, though, it could also apply to a page like http://yourdomain.com/about/old-page/.
This is especially important if your page URL is relatively generic and could realistically show up in the middle of another URL, like “about” or “product.” By forcing a start and end to your expression, you save a lot of headache with accidental matches hidden throughout your site.
Making trailing slashes optional
You may have noticed that the previous example applies to “/old-page/” with a trailing slash, but not to “/old-page” without a trailing slash. Since many content management systems are wishy-washy about whether they force a trailing slash, force no trailing slash, or allow both versions, your redirects will always need to account for either version of each URL.
Here’s how to use a single regular expression to apply to a page whether it has a trailing slash or not:
We’ve added [/]? (slash in brackets with a question mark after it) to indicate that this character should be treated as optional. If you were to read this expression out loud, it’d sound something like this:
- ^ = Start of URL
- /old-page = This must appear in the URL right after the starting point
- [/]? = At this point, there may or may not be a slash
- $ = End of URL
This technique allows you to create one redirect that covers two possible URLs (with and without a slash). The next technique allows you to really go wild.
Creating wildcard redirects
A wildcard redirect allows you to create a redirect based on any page that matches a pattern, regardless of the URL of a specific page. This is especially handy for redirecting whole directories or sections of an old site to a new site. For example, I recently used this to redirect all URLs starting in “/products/” (plural) to the corresponding URL on the new site, which started with “/product/” (singular).
Here’s the regular expression for the pattern to match (Source URL):
- ^ = Start URL
- /old/ = URL must contain this string at the beginning
- (.*) = Wildcard. This matches anything that appears after /old/ and saves it for use in the Target URL below.
- $ = End URL. This is not really crucial since we’re picking up anything at the end as a wildcard, but it doesn’t hurt.
And here’s the destination (Target URL):
The $1 outputs the first wildcard in your Source URL expression. In theory, you could have an unlimited number of wildcards, like this:
While that’s not a super-common requirement, be aware you have the flexibility to use as many wildcards as necessary.