1. Home
  2. Docs
  3. Plugin settings
  4. Trailing slashes

Trailing slashes

Generally, the URLs pointing to a directories end with a slash and files’ URLs do not have them. In practical terms, on some of WordPress websites the trailing slashes are added to all permalinks. While the rest of them does not use them at all.

# URLs without a trailing slash
http://example.com/wp-content/uploads/2020/01/sample-image.jpg
http://example.com/blog/sample-wordpress-post-ends-without-trailing-slash

# URLs with a trailing slash
http://example.com/wp-content/uploads/2020/01/
http://example.com/blog/another-wordpress-post-ends-with-trailing-slash/

Control trailing slashes in WordPress permalinks

You can use Permalink Manager to either add or remove the trailing slashes from all WordPress permalinks. The new settings will be globally applied to all posts, pages, categories and term URLs.

Based on your preference, in the plugin settings you can either choose to:

  • keep native settings
  • force trailing slashes (and if needed auto-redirect permalinks without them) in all WordPress permalinks
  • remove trailing slashes (and if needed auto-redirect permalinks with them) from all WordPress permalinks
You can get back to original mode at any moment! To revert the changes please select “Use default settings” in Permalink Manager settings.

How does it work?

The plugin filters $wp_rewrite->use_trailing_slashes property/variable, so all WordPress permalinks (not only the ones filtered with Permalink Manager) will be affected.

Conditional trailing slashes

The trailing slashes mode will be forced with Permalink Manager to all WordPress permalinks. Nevertheless, you can change this programmatically and remove/add the slashes for specific post types or taxonomies.

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

Add the trailing slashes to “categories” and “product_cat” (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 the trailing slashes from “posts” and “product” (custom post type) 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 enabled “Trailing slash redirect” option and used one of snippets above you will need another one. Normally, the redirect function will force or remove the slash from all WordPress permalinks. To avoid the redirect loop, you should exclude the permalinks where the slashes should be added or removed conditionally (differently than the rest of permalinks).

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);