Have you ever noticed how some websites have URLs that end with a forward slash, while others don’t? For example, “sampledomain.com/page” and “sampledomain.com/page/” might look pretty much the same to us, but search engines see them as two different pages. That’s where the trouble starts!
One of those has a trailing slash, and the other doesn’t. A trailing slash is the forward-slash (/) character placed at the end of the URL. The primary purpose of adding it is to help distinguish between a directory with the trailing slash and a file that doesn’t.
See, even if both URLs contain identical content, search engines like Google will treat them as separate pages. This can cause problems with duplicate content and hurt your search engine rankings. The reason for this is that search engines can’t always tell which URL is the “original” one, so they treat them both as unique pages.
So, how do you fix this problem? The best way is to use what’s called a “redirect”. This means that you’ll set up your website so that if someone tries to access a URL without the trailing slash, they’ll automatically be redirected to the version that does have it.
Redirects are a simple but effective way to ensure that search engines don’t see multiple versions of the same page. It’s like telling Google, “Hey, this page is actually the same as that one – don’t treat them as separate pages!”
Now, you might be wondering how to actually set up these redirects. It’s
not as complicated as you might think! There are plenty of resources out there that can walk you through the process step-by-step. The more complicated the need, the more complicated the ReGex rule is going to be. It’s definitely worth taking the time to do this, as it can have a big impact on your website’s SEO.
What this means for WordPress
WordPress uses trailing slashes by default, which means that if you try to access a URL without the trailing slash, WordPress will automatically redirect you to the version that does have it. You can change this in the custom permalink settings, but I wouldn’t recommend it.
WordPress only does automatic redirects for pages/posts that currently exist on the site. So if you’ve got a redirect you want to make from yoursite.com/oldpage/ to yoursite.com/newpage/, if a user (or someone in your team) enters this without the trailing slash, ie, yoursite.com/oldpage, the user will be taken to a 404. That’s not great.
So let’s create redirects that work regardless of whether a user enters a trailing slash or not? That’s where regular expressions (or “ReGex” for short) come in.
ReGex is a powerful tool that allows you to match patterns in text. In the case of URL redirects, you can use ReGex to match any URL that contains a certain pattern (like a specific page or post), regardless of whether it ends with a slash or not. This can be especially useful if you’re dealing with a large number of pages or posts on your site.
To create a ReGex redirect, you’ll need to use a special syntax to define your pattern. Here’s an example that you’d stick into your .htaccess file if you’re running apache as a server:
RedirectMatch 301 "^/blog/oldpage/?$" "https://www.example.com/newpage/"
This redirect will match any URL that starts with “/blog/oldpage“, and then either ends with a slash or doesn’t. The “?” character after the forward slash tells the server to treat it as optional. Now, whether someone enters “/blog/oldpage” or “/blog/oldpage/“, they’ll be redirected to the new page.
What about Query Parameters?
When writing ReGex rules for trailing slashes, it’s important to take into consideration whether a link has a query parameter appended on the end. This can be a bit trickier, as query parameters can cause the ReGex to fail if not properly accounted for.
To redirect a URL that does not have a trailing slash, but does include a query parameter and you want the query parameter to be preserved for the redirect, you can use a regular expression to match the URL and then use the captured query parameter in the redirect.
It should be noted that within WordPress you don’t need to do this where the main basis of the URL is the same, because WordPress will automatically handle the trailing slash redirect, and add on the query parameter to the page permalink. So https://sample.com/blog?hs=emailblast becomes https://sample.com/blog?hs=emailblast.
What about cases where the source url and the target url are different? For example if you’re trying to get https://studiok40.com/news?hs=emailblast to go to https://studiok40.com/blog/?hs=emailblast. What then?
If you’re using the Redirection plugin, follow these instructions for success.
- Install and activate the Redirection WordPress plugin if you haven’t already done so.
- Go to the Redirection settings page in your WordPress dashboard by clicking on “Tools” and then “Redirection”.
- Click on the “Add New” button to create a new redirect rule.
- In the “Source URL” field, enter the regular expression that matches the specific URL you want to redirect. For example, if you want to redirect all URLs that start with “/old-page/” and have any number of query parameters, you can use the following regular expression:
- In the “Target URL” field, enter the URL of the new page that you want to redirect to. You can use
$1in the target URL to capture and pass along any query parameters from the original URL. For example, if you want to redirect to “/new-page/” and pass along any query parameters from the original URL, you can use the following target URL:
- Select the “Regular Expression” checkbox to enable regular expression support.
- In the “Match” dropdown, select “URL only” to match the entire URL including any query parameters.
- In the “Action” dropdown, select “Redirect to URL” to specify that you want to redirect to a new URL.
- Click on the “Add Redirect” button to save your new redirect rule.
Breakdown of those regular expression characters in
^– This symbol is an anchor that matches the start of a string. It is used to indicate that the regular expression should only match URLs that start with the specified pattern.
/old-page/– This part of the regular expression matches the literal string “/old-page/”. It is used to match the specific URL you want to redirect from.
?– This symbol makes the preceding character or group optional. In this case, it makes the trailing slash optional, so that URLs with or without a trailing slash will match the pattern.
(.*)– This part of the regular expression is a capture group that matches any number of characters. The
.matches any character, and the
*quantifier means “match zero or more times”. The parentheses capture the matched characters so they can be used later in the target URL.
$– This symbol is an anchor that matches the end of a string. It is used to indicate that the regular expression should only match URLs that end with the specified pattern. This is optional, and should be based on your usage need. When we use
$, we say that any user URL entry that matches the original and includes a subdirectory will be ignored and not redirected. Thus, if a user entered
sampledomain.com/old-page/childpage/grandchildthey would NOT be redirected. If you wanted these nested directory pages to also get redirected, leave off the
Explanation for the Target URL inclusion of the symbols
In regular expressions, the symbol
$1 is a placeholder that refers to the first capture group in the regular expression pattern. It is used in the target URL of a redirect rule to reference and include the captured content from the first capture group in the redirected URL.
Capture groups are used to capture and store specific parts of the matched pattern in regular expressions. They are enclosed in parentheses
( ) within the regular expression pattern. In the case of the target URL of a redirect rule,
$1 refers to the content that was captured by the first capture group in the source URL pattern. So, when a URL matches the source URL pattern, the captured content from the first capture group will be included in the target URL, replacing
$1 in the final redirected URL.
Examples in Action
Overall, this regular expression matches URLs that start with “/old-page/”, optionally followed by a trailing slash, and captures any additional characters (including query parameters) into a capture group.
Go try it for a couple examples on my own site.
- https://studiok40.com/tips?hs=newsletter works the same as if it had a trailing slash in it for https://studiok40.com/tips/?hs=newsletter
- Similarly, a different query parameter for a different tracking purpose works just as well https://studiok40.com/news?ga=instagram-post works as well as https://studiok40.com/news/?ga=instagram-post
As always, it’s important to thoroughly test your rules to ensure that they’re working as intended. Overall, using ReGex redirects can be a powerful way to ensure that your site is structured in a way that’s both user-friendly and search engine-friendly. So if you’re looking to take your site’s SEO to the next level and simultaneously improve the user experience, give them a try!