Redirection to Enforce Trailing Slash on URLs

If you need to enforce a trailing slash at the end of your URLs you can use the following MU plugin. Note that as long as your canonical urls are correctly set, Google will index the desired url even without this fix.

1
Download the following file: wprocket-slash.php
2
Upload the file to: wp-content/mu-plugins
3
If the mu-plugins folder doesn't exist, create it.
4
Once done, de-activate and re-activate WP Rocket.

Alternatively you can add the following code to the functions.php of your theme, then de-activate / re-activate WP Rocket:

add_filter( 'before_rocket_htaccess_rules', '__force_trailing_slash' );
function __force_trailing_slash( $marker ) {
	$redirection  = '# Force trailing slash' . PHP_EOL;
	$redirection .= 'RewriteEngine On' . PHP_EOL;
	$redirection .= 'RewriteCond %{REQUEST_FILENAME} !-f' . PHP_EOL;
	$redirection .= 'RewriteCond %{REQUEST_URI} !(.*)/ . PHP_EOL;
	$redirection .= 'RewriteRule ^(.*)$ http' . ( is_ssl() ? 's' : '' ) . '://%{HTTP_HOST}/$1/ [L,R=301]' . PHP_EOL . PHP_EOL;
	$marker = $redirection . $marker;

	return $marker;
}