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 M3CODE
RewriteCond %{HTTP_HOST} ^m3server.com [NC]
RewriteRule ^(.*)$ https://www.m3server.com/$1 [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# END M3CODE

PLACE YOUR OTHER HTACCESS DIRECTIVES BELOW THIS ONE IF APPLICABLE

This example forces www to non www https.

# BEGIN M3CODE


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

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# END M3CODE

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

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

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

 

World Class Web Hosting
m3server.com

About the author

Level 1 Support

Level 1 Technical Support