Force https – htaccess rewrite non ssl to ssl

Want to force users to use SSL on your site?

Add this code to the top of your .htaccess file, edit our domain with your own.  This htaccess file should be at your site’s root directory, or the subdirectory if you prefer further customization and control.

WARNING:  Make a backup of your htaccess file so you can quickly fix your site if things go wrong such as typos!!

This example forces the non www to www to https and forces www to https.

# BEGIN CODE
RewriteCond %{HTTP_HOST} ^m3server.com [NC]
RewriteRule ^(.*)$ https://www.m3server.com/$1 [L,R=301]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.m3server.com/$1 [R=301,L]

# END CODE – PLACE YOUR OTHER HTACCESS DIRECTIVES BELOW THIS ONE IF APPLICABLE

This example forces www to non www https.

# BEGIN CODE
RewriteCond %{HTTPS} off
RewriteRule (.*) https://m3server.com%{REQUEST_URI} [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^www.m3server.com [NC]
RewriteRule (.*) https://m3server.com%{REQUEST_URI} [L,R=301,NC]

# END CODE

Note:  You only need one “RewriteEngine On” directive per htaccess file.

If you place this code at the top you can exclude subdomains from being force redirects to non www version of site.

RewriteCond %{HTTP_HOST} !^(cdn|media)\. [NC]

To avoid issues with browser caching you can check redirect at the link below.

https://www.redirect-checker.org/

 

World Class Web Hosting
m3server.com

Posted in .