<?php
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );
/**
 * Plugin Name: WP Rocket | Fix non-www to www and non-SSL to SSL redirections.
 * Description: Fixes redirection from non-www to www, and non-SSL to SSL 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_ssl_www_redirection' );
function __fix_wprocket_non_ssl_www_redirection( $marker ) {
    $redirection  = '# Redirect non-www to (ssl) www' . PHP_EOL;
    $redirection .= 'RewriteEngine On' . PHP_EOL;
    $redirection .= 'RewriteCond %{HTTP_HOST} ^example\.com [NC]' . PHP_EOL;
    $redirection .= 'RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]' . PHP_EOL . PHP_EOL;
    $redirection .= '# Redirect non-SSL to SSL' . PHP_EOL;
    $redirection .= 'RewriteCond %{HTTPS} !on' . PHP_EOL;
    $redirection .= 'RewriteCond %{SERVER_PORT} !^443$' . PHP_EOL;
    $redirection .= 'RewriteCond %{HTTP:X-Forwarded-Proto} !https' . PHP_EOL;
    $redirection .= 'RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]' . PHP_EOL . PHP_EOL;
    return $redirection . $marker;
}
