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.

Download the following file: wprocket-slash.php
Upload the file to: wp-content/mu-plugins
If the mu-plugins folder doesn't exist, create it.
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;