Fixing non-www Redirection

In this Article

Heads up! Don’t forget to replace all appearances of example.com or example\.com with your own domain name in the code examples of this article!

Redirection non-www → www

If you have a problem with the redirection of the non-www to the www version of your URL, use the following fix as a MU plugin:

1
Create a folder named mu-plugins in your wp-content folder.
In case a mu-plugins folder already exists, don’t create a new one, just use the existing one.
2
Using a basic text editor create a new file and put this code in it:
<?php
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );
/**
 * Plugin Name: WP Rocket | Fix non-www to www redirection
 * Description: Fixes redirection from non-www to www URLs.
 * Author:      your name here
 * License:     GNU General Public License v3 or later
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */
add_filter( 'before_rocket_htaccess_rules', '__fix_wprocket_non_www_redirection' );
function __fix_wprocket_non_www_redirection( $marker ) {
	$redirection = '# Redirect non-www to www' . PHP_EOL;
	$redirection .= 'RewriteEngine On' . PHP_EOL;
	$redirection .= 'RewriteCond %{HTTP_HOST} ^example\.com [NC]' . PHP_EOL;
	$redirection .= 'RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]' . PHP_EOL . PHP_EOL;
	$marker = $redirection . $marker;
	return $marker;
}
	
3
Save the file as wp-rocket-non-www-fix.php
4
Upload this file to the mu-plugins folder created in step 1.
5
Finally, make sure to deactivate and re-activate WP Rocket.

If you prefer to add code to the functions.php of your theme, you can use the following code snippet instead:

add_filter( 'before_rocket_htaccess_rules', '__fix_wprocket_non_www_redirection' );
function __fix_wprocket_non_www_redirection( $marker ) {
	$redirection = '# Redirect non-www to www' . PHP_EOL;
	$redirection .= 'RewriteEngine On' . PHP_EOL;
	$redirection .= 'RewriteCond %{HTTP_HOST} ^example\.com [NC]' . PHP_EOL;
	$redirection .= 'RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]' . PHP_EOL . PHP_EOL;
	$marker = $redirection . $marker;
	return $marker;
}

Remember to deactivate and re-activate WP Rocket after adding the above code.

Redirection www → non-www

If you need the opposite rule (www to non-www), use this code for your MU plugin:

<?php
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );
/**
 * Plugin Name: WP Rocket | Fix www to non-www redirection
 * Description: Fixes redirection from www to non-www URLs.
 * Author:      your name here
 * License:     GNU General Public License v3 or later
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */
add_filter( 'before_rocket_htaccess_rules', '__fix_wprocket_non_www_redirection' );
function __fix_wprocket_non_www_redirection( $marker ) {
	$redirection = '# Redirect www to non-www' . PHP_EOL;
	$redirection .= 'RewriteEngine On' . PHP_EOL;
	$redirection .= 'RewriteCond %{HTTP_HOST} ^www.example\.com [NC]' . PHP_EOL;
	$redirection .= 'RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]' . PHP_EOL . PHP_EOL;
	$marker = $redirection . $marker;
	return $marker;
}

Or this code for your functions.php:

add_filter( 'before_rocket_htaccess_rules', '__fix_wprocket_non_www_redirection' );
function __fix_wprocket_non_www_redirection( $marker ) {
	$redirection = '# Redirect www to non-www' . PHP_EOL;
	$redirection .= 'RewriteEngine On' . PHP_EOL;
	$redirection .= 'RewriteCond %{HTTP_HOST} ^www.example\.com [NC]' . PHP_EOL;
	$redirection .= 'RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]' . PHP_EOL . PHP_EOL;
	$marker = $redirection . $marker;
	return $marker;
}

Remember to deactivate and re-activate WP Rocket after adding the above code.