Apache redirect to SSL & WWW

Print Friendly, PDF & Email

My site’s SSL certificate is only valid for WWW, so for me it was important to both force use of the certificate I’m paying for, and to prevent the annoying “Certificate is only valid for www.k33bz.com” error. To accomplish this, I wanted it to be seamless for the end user, and as easy as possible to maintain for me. I chose to use the Apache Rewrite option using the standard .htaccess file since I wouldn’t need to modify any PHP code in WordPress itself. To accomplish this, here is the code I used:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]