How to edit trailing slashes in WordPress permalinks?

Trailing slash settings might cause SEO issues with duplicate content if they are not properly handled. While WordPress is generally user-friendly, this problem doesn't seem to have any intuitive inbuilt solution.

The forward slash at the end of a website address is called a trailing slash. By design, URLs for folders or directories are meant to end with a slash. This helps distinguish them from single file URLs like images, which typically end with a file extension like .jpg or .css instead of a slash.

However, CMS such as WordPress can dynamically change URLs on the fly. So this rule about trailing slashes is not always followed as strictly as it used to be. The CMS might add or remove that slash depending on what helps people find the page more easily. So both folder URLs with a trailing slash and single file URLs without one can work fine nowadays.

Although trailing slashes can have a big influence on SEO, WordPress website owners have no direct control over whether or not they are included to their permalinks. There is, however, a method that allows website owners to control trailing slashes using the WordPress UI without the need for extra plugins.

Does trailing slash matter for SEO?

Considering the possible effect on SEO that trailing slashes in WordPress permalinks might have, it is surprising how little information there is on the subject. Happily, there is nothing complicated or hard to understand here.

Depending on the permalinks settings, trailing slashes may be automatically appended or removed from all URL addresses. Take a close look at these URLs below. Both look identical, but only at first glance. The first URL varies from the second in that it finishes with a slash.

Trailing slash in WordPress URL (example)
The single slash character (/) is what sets the URLs apart.

Most visitors to your website likely will not notice whether URLs include a trailing slash or not. It might, however, make a significant difference for search engine crawlers. Search bots may consider the second variant of URL as a duplicate if they can reach the same page via separate URLs.

To sum up, for SEO, even details like this can make a big difference. Years ago, John Mueller, one of most well-known Google experts, emphasized on Twitter how important it is to be consistent with trailing slashes.

Trailing slash redirect

Regardless of whether you decide to include them in the URL or not, you need ensure that all of the URLs follow your choice and use the same settings. Redirecting non-trailing slash URLs to trailing slash URLs or vice versa is an effective way to ensure this.

Canonical URLs are handled by WordPress to some extent, however the functionality may vary based on your settings, plugins, and WordPress version. However, the canonical redirect can also be affected by factors such as your server's setup, caching systems, and additional plugins.

How to use the trailing slash redirect for WordPress permalinks?

Even if you do not know how to code, you can use Permalink Manager to force the preferable trailing-slashes mode with the "trailing slash redirect" function.

Google Analytics reports and trailing slashes inconsistency

In addition, if your website handles trailing slashes inconsistently, it is possible that duplicate URLs may appear in your Google Analytics results. While past statistics cannot be fixed, you can adjust the future collection of data to avoid this issue.

Google Analytics dashboard

Thankfully, it can be avoided with a straightforward fix. The first option is to apply the URL filters that are included into Google Analytics. There are numerous articles, such as this one, that explain how to use them.

However, forcing one version of the URL via a "301" redirect directly on your server is the most reliable and recommended method. You may accomplish this by using .htaccess code or a third-party plugin like Permalink Manager.

How to control trailing slash settings in WordPress without a plugin?

There is a simple way to keep your trailing slash settings consistent without having to install extra plugins. Most WordPress users and developers are unaware that the trailing slash mode is determined by whether a slash appears at the end of the "Custom Sturcture" field.

You may easily adjust it using the built-in permalink settings. Even users who are unfamiliar with WordPress should find this simple. To do so, go to the "Settings -> Permalinks" section and, depending on your situation, either add or delete the last slash.

"Custom Structure" field affects your trailing slash mode.
The "Custom Structure" field ends with a slash, so all other WordPress URLs will have the trailing slash. Likewise, if it is not present there, the slash will be missing in your website's URLs.

How to add trailing slashes?

Please see the screenshot below to see how to add trailing slashes to WordPress permalinks using only the built-in settings. Because the structure field ends in a slash (see red arrow), WordPress will append the slash to all URLs.

Add trailing slash

How to remove trailing slashes?

The steps are similar to the previous section. To remove the trailing slash from WordPress permalinks, simply ensure the "Custom Structure" field does not end with a forward slash.

Remove trailing slash from WordPress permalinks

Permalink Manager may be a useful solution to modify trailing slashes settings if you do not have any technical skills or simply do not want to bother with coding. To put it in a nutshell, you may use the plugin to either add or delete the slashes automatically from all WordPress permalinks.

The plugin dynamically filters $wp rewrite->use_trailing_slashes property, therefore all WordPress permalinks (not only the ones filtered with Permalink Manager) will be affected. Of course, this also applies to meta tags (canonical URLs) and sitemaps built by third-party tools like Yoast SEO or RankMath.

You can get back to original mode at any moment! To revert the changes please select "Use default settings" in Permalink Manager settings.

"Trailing slashes" settings in Permalink Manager admin panel.

How to force trailing slash mode with redirect

A canonical redirect is the quickest solution to address duplicate content issues caused by trailing slashes. If you choose to implement the 301 redirect, both users and search engine crawlers will always be sent to to one of two URL versions: with or without slashes.

If you do not know how to code, Permalink Manager may help you in using the redirect. To make it work and force your chosen slashes configuration, enable the "Trailing slashes redirect" option in the plugin settings.

Trailing slash redirect checkbox
To activate the redirect, please turn on "Trailing slashes redirect" option in "Settings -> Redirect settings" panel.

Trailing slashes and cache plugins

Certain caching plugins may cause the Permalink Manager trailing slash redirect to fail. It happens, for example, when you use the WP Rocket plugin. This is because cache plugins will utilize the same cache container for the URL with and without the trailing slash.

As a consequence, Permalink Manager will be unable to determine if the URL has a slash or not due to the cache. To fix this problem, add one of the following snippets to the beginning of the .htaccess file.

If you need to remove the trailing slashes from all URLs, use the following code:

#
# A. Remove trailing slashes
#
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

The code to force the trailing slashes is a little more complicated, as we need to exclude filenames and URLs with query parameters.

#
# B. Force trailing slashes
#
RewriteEngine on
# Exclude files and directories under "wp-content", "wp-admin", "wp-json" and URLs with query parameters
RewriteCond %{REQUEST_URI} !^/?wp-(admin|json|content)
RewriteCond %{QUERY_STRING} ^$
# This rule should not be applied to URLs that look like filenames and already end with a trailing slash
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
# Check if the request URI does not end with a slash
RewriteCond %{REQUEST_URI} !(.+)/$
# If all conditions are met, redirect to the same URI with a trailing slash
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

How to add/remove slashes conditionally?

As indicated before, the user-selected mode will be forced all WordPress permalinks. Nevertheless, you may adjust this programmatically and remove/add the slashes for certain post types or taxonomies.

Below you can find an example how to remove the trailing slashes from all permalinks with the exception of chosen taxonomies and post types. The following code snippet should be added to your (child) theme’s functions.php file.

Add trailing slash to URL WordPress (custom taxonomy) permalinks:

function pm_term_permalinks_trailing_slashes($permalink, $term) {
	if(!empty($term->taxonomy) && (in_array($term->taxonomy, array('category', 'product_cat')))) {
		$permalink = trailingslashit($permalink);
	}
	return $permalink;
}
add_filter('permalink_manager_filter_final_term_permalink', 'pm_term_permalinks_trailing_slashes', 999, 2);

Remove trailing slash from URL WordPress (post & product) permalinks:

function pm_post_permalinks_trailing_slashes($permalink, $post) {
	if(!empty($post->post_type) && (in_array($post->post_type, array('post', 'product')))) {
		$permalink = untrailingslashit($permalink);
	}
	return $permalink;
}
add_filter('permalink_manager_filter_final_post_permalink', 'pm_post_permalinks_trailing_slashes', 999, 2);

Prevent redirect loop (trailing slash redirect)

Finally, if you activated the "Trailing slash redirect" option and used one of the above snippets, you will require another one. The redirect function will normally force or remove the slash from all WordPress permalinks. To avoid the redirect loop, deactivate programatically the redirect for permalinks where slashes should be used conditionally.

function pm_stop_trailing_slash_redirect($correct_permalink, $redirect_type, $queried_object) {
	if(!empty($queried_object->post_type) && (in_array($queried_object->post_type, array('post', 'product'))) && $redirect_type == 'slash_redirect') {
		return false;
	}
	return $correct_permalink;
}
add_filter('permalink_manager_filter_redirect', 'pm_stop_trailing_slash_redirect', 9, 3);
Go up