The understanding of how to implement redirects using Regular Expressions can be frustrating, even for experienced developers. Regular Expressions are essentially their own language, and they vary slightly between contexts.
For example, you can use them in PHP, but they work a bit differently in JavaScript and even vary when performing 301 redirects through .htaccess or a plugin.
Add the fact that it’s not something most developers deal with daily, and you end up with a lot of trial and error that takes more time than it should.
In this post, we’ll focus on constructing effective 301 redirects (or any other type of redirect) using Regular Expressions. The method shown in this guide allows you to achieve redirects with less effort and less manual input, covering an unlimited number of URL addresses with a single rule.
In the next post, I explain what 301 redirects are. If you’re not sure about them, take a look…
Redirection Plugin for WordPress
You surely know that redirecting a URL that is no longer in use to a new page is essential for SEO. Performing redirects allows you to transfer the existing link equity from the old address to the new one.
In many cases, when rebuilding a website, you’ll need to perform redirects extensively. It’s also an action you’ll take from time to time during maintenance of an existing site, for example when a page is removed or its URL is changed.
There are many ways to approach redirects, but the simplest way to manage them in WordPress is by using the Redirection plugin by John Godley.
Be aware that there are plugins with similar names. Make sure you’re using this specific plugin for the purpose of this guide.
Although the plugin is far from perfect, and ideally redirects should be done at the server level without a plugin, it offers several advantages:
- You can manage all redirects from the WordPress admin interface, allowing non-technical users to view and update redirects as needed.
- You can track how many times a certain redirect is used and even monitor 404 errors.
- It works even if your server doesn’t run Apache or doesn’t allow editing the
.htaccessfile.
Installing the Redirection Plugin
Install and activate the Redirection plugin and go to Tools > Redirection to get started.

In addition to the advantages listed above, the Redirection plugin eliminates the need to touch server configuration files and supports automatic redirects when a post’s URL changes.
For performance reasons, it’s preferable to perform redirects at the server level. However, the performance difference is very minimal in practice.
Regular Expressions and Redirects – Prefix and Suffix
Two main components of redirects with Regular Expressions are the first and last characters – the caret (^) and the dollar sign ($).
These allow you to specify the start and end of the expression and prevent many errors that cause redirect loops. Here’s an example:
^/old-page/$And here’s how it looks in the Redirection plugin:

Note that the Regular Expressions checkbox is marked so that the plugin recognizes the regular expressions.
This expression will only apply to the exact format of http://domain.com/old-page/. If we didn’t add the caret (^) and dollar sign ($), the redirect would also fire for http://domain.com/about/old-page/.
This is especially important when a word in the URL is generic and may appear in other URLs, such as about or product. By adding these anchors, you prevent unwanted matches to other addresses on your site.
Always use ^ and $ anchors in your regex redirects. Without them, a redirect for /product/ could accidentally match /new-product/, /product-reviews/, or any other URL containing that string, creating redirect loops or broken pages.
What about that Trailing Slash?
You may have noticed that the example above matches /old-page/ with a trailing slash, but it does not match /old-page without one.
Since many sites use the trailing slash at the end of URLs and many don’t, this must be taken into account when creating redirects.
Take a look at my post on the importance of the trailing slash in URLs for more context.
Here’s a regular expression that matches the page both with and without the trailing slash:
^/old-page[/]?$We added [/]?$ to indicate that the trailing slash is optional. Let’s break this down:
^= The start of the URL (after the domain name)./old-page= Must appear directly after the start.[/]?= At this point, a “/” character may or may not appear.$= The end of the URL.
This technique allows you to create a single redirect that covers both URL variations. However, the technique in the next section is even more powerful.
Using Wildcards for Redirects
The wildcard redirect lets you create a redirect based on any page matching a certain pattern, without targeting a specific URL.
This is especially useful for redirecting entire directories or sections of an old site to a new one. For example, you can redirect all URLs starting with /products/ (plural) to the matching URL under /product/ (singular).
Here’s the regular expression for the source URL:
^/old/(.*)$^= The start of the URL./old/= Must appear directly after the start.(.*)= Wildcard. Matches any string after/old/and captures it for use in the target URL.$= The end of the URL.
And here’s the target URL:
/new/$1The $1 part outputs the string captured by the wildcard in the source URL. You can use multiple wildcards, like in this example:
^/old/(.*)/old-section/(.*)This would redirect to:
/new/$1/new-section/$2While this situation isn’t common, know that this flexibility is available. You can build redirects more complex than a simple one-to-one mapping.
Testing and Implementing Redirects
The explanations above should cover most redirect scenarios. To avoid issues, follow these points when creating redirects in WordPress:
- Before testing, make sure the cache is cleared locally and on the server.
- Use Incognito/Private Browsing mode because browsers sometimes store redirects. If you use a caching plugin like WP Rocket, server-level caching, LiteSpeed Cache, or Cloudflare, make sure to clear those caches as well.
- Avoid excessive redirect chaining. Try to keep a maximum of one redirect for a URL. Chains slow down page loads and dilute link equity.
- If you encounter a redirect loop while using the Redirection plugin, check for conflicts with other redirects, such as those in the
.htaccessfile. Redirects through.htaccessare executed before plugin-level redirects. - Test your regex patterns with an online regex tester before deploying them. Paste the pattern and a few sample URLs to confirm it matches what you expect and nothing else.
Some managed WordPress hosts (such as WP Engine) have deprecated .htaccess support and use their own redirect management tools instead. If your host doesn’t support .htaccess, the Redirection plugin is a reliable alternative that works independently of server configuration.
FAQs
Common questions about redirects and regular expressions in WordPress:
.htaccess for redirects?
.htaccess are slightly faster because they are processed before WordPress loads. However, the performance difference is minimal. The Redirection plugin is easier to manage, provides a visual interface, and works on servers that don't support .htaccess. For most sites, the plugin is the more practical choice.^ and $ anchors that matches too broadly. Check all active redirects in both the plugin and .htaccess for conflicts.(.*) mean in a regex redirect?
(.*) pattern is a wildcard that matches any character (the dot) zero or more times (the asterisk). The parentheses capture the matched string so you can reuse it in the target URL with $1. For example, ^/old/(.*)$ redirected to /new/$1 maps /old/shoes/ to /new/shoes/.^/old-blog/(.*)$ to /blog/$1 can handle hundreds of redirects at once, rather than creating individual redirects for each URL. This is one of the biggest advantages of using regex for redirects..htaccess or your server's configuration.Summary
I hope this guide shed some light on redirects and regular expressions. Using regex can save you a lot of time and prevent mistakes, especially when migrating a site or restructuring URLs.
The key patterns to remember: use ^ and $ to anchor your expressions, [/]? to handle optional trailing slashes, and (.*) with $1 for wildcard redirects. Test your patterns before deploying, clear your caches, and watch for redirect loops.
If you have smarter ways, or new approaches I didn’t cover here, share them in the comments. Good luck! 🙂

