How to edit trailing slashes in WordPress permalinks?

Trailing slashes might cause SEO issues with duplicate content if they are not properly handled. Unfortunately, the WordPress interface does not make things any simpler.

The forward slash at the end of a URL is known as a trailing slash. Generally, all directory URLs should end with a trailing slash, although single file URLs (e.g. images) should not. As far as trailing slashes in WordPress permalinks are concerned, this is not always the case.Depending on the permalinks settings, trailing slashes may be automatically appended or removed from all URL addresses.

Does trailing slash matter for SEO?

Do you wonder if the trailing slash makes any difference? If that’s the case, have a look at the two URLs listed below.

They appear to be nearly identical at first glance. If you do not have your WordPress redirect one to another, Google will consider them as different pages with the identical content. In other words, if the same page can load on two separate URLs we run into duplicate content difficulties. Although the URLs seem to be equal to your website users, the slash at the end of the URL makes a significant difference to search engines like Google! If you are still not convinced, look at one of John Mueller’s tweets, one of Google’s most well-known specialists, in which he addressed the problem.

Trailing slashes and SEO
If you’re interested in learning more about this issue, the Ahrefs blog has a comprehensive article on this topic.

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 trailing slashes automatically from all WordPress permalinks.

How to control trailing slashes 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. 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 trailing slash will be missing in your website’s URLs.

Certain, the tips outlined above are beneficial to a large number of users. The WordPress code, however, does not ensure that visitors and Google will be automatically redirected to a single, chosen version of the URL (with or without the trailing slash). The inconsistent use of trailing slashes in URLs may have a negative SEO impact. The Permalink Manager’s “trailing slash redirect” functionality can be really helpful in this circumstance. More information is provided later in this article.

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 trailing 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:

# Remove trailing slashes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Force trailing slashes
RewriteEngine on
RewriteCond %{REQUEST_URI} !(/$|.) 
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