Setting up Wildcard 301 Redirects with WordPress
If you are switching domains (e.g. going from oldexample.com to newexample.com), luckily you don’t have to create a 301 redirect for every page and post on the site. Whew! That could take a while for large sites. Wildcard redirects allow you to redirect all of the URLs on your site to another domain with a single redirect rule. SEM Rush says this:
- Wildcard redirects can also be used when there is a single variable changing — for example, one folder name. The rest of the URL path must be kept consistent to use a wildcard.
- If you are changing URLs in bulk and there is only a single change being made in the path, wildcards can be an option to redirect all those within a subfolder without the need to implement an individual redirect for each.
- If there is more than one variable in a path changing, redirects wouldn’t be suitable.
When we built the website for the Wisconsin Bankers Association, we were migrating from an old CMS to WordPress. The website had hundreds of old posts, some of which were labeled “Press Releases” and some of which were called “Articles.” Here are examples of two of the old URLs.
https://www.wisbank.com/press-releases/2020/05/wba-press-release-can-i-wear-a-mask-into-my-bank
https://www.wisbank.com/articles/2021/10/advocacy-on-regulatory-modernization
On the new site, we wanted both the Articles and the Press releases to live on the Blog. The new URLs that we wanted to create looked like this:
https://www.wisbank.com/2020/05/wba-press-release-can-i-wear-a-mask-into-my-bank
https://www.wisbank.com/2021/10/advocacy-on-regulatory-modernization
In other words, we wanted to remove the words “press-releases” and “articles” from the URLs. And we didn’t want to have to manually create hundreds of 301 redirects!
Enter the wildcard redirect! Wildcard redirects are a slightly more advanced feature that let you dynamically redirect multiple pages with a single redirect rule.
Using the Redirection WordPress plugin and the built-in permalink structure in WordPress, we were able to easily achieve our mission.
First we changed the Permalinks from “Post name” to include the date with the “Month and name” option. Lucky for us, the date format was the same as the old platform!
(Note, please ignore the fact that those URLs in that screenshot don’t have https. They should!)
Now you might think that we could have added “/articles…” in the Custom Structure permalink, but because we also had to deal with press releases, we also needed the wild card redirect.
Using the Redirection plugin, we created the following rule:
Choose “Regex” in order to use regular expressions in the rule. Also, it’s good practice to always “Ignore Slash” and “Ignore Case” unless you have a good reason not to!
In the Source URL field: /articles/ represents the part of the URL that we are trying to change.
The ^ is there to help prevent infinite redirect loops.
The (.*) is the wildcard / dynamic information, that indicates …and whatever comes after /articles/.
Then in the Target URL, we can use the /$1 to indicate that we are replacing /articles/ with nothing. And then we are taking what was stored in the wildcard and adding it.
Then we made a similar redirect rule for the press release section by simply replacing “articles” with “press-releases”:
Once those rules were in, we tested it by trying to navigate to some of the old URLs, and they worked!
Note: If you aren’t seeing your redirect work right away but you are pretty certain it should work, you may need to delete your htaccess file via FTP or the File Manager. (As always, download a copy before deleting it just in case!)
Resources to learn more:
Understanding Redirection Regular Expressions
Adding 301 Redirects in WordPress