Redirect To SSL Using Apache's .htaccess



If you want your visitor to be accessing a site only via SSL, you can add the code below into your .htaccess.

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.example.com/require-secure/ [R]

The first line tells Apache we are going to use mod_rewrite. The second line only matches if the port being used to access the site is 443 (the port reserved for https use). If that second line matches then the third takes kicks in, which simply redirects the user to the SSL version of your URL. This still enforces the use of SSL, but saves you from trying figure why you can't get to your site just because you forget the s in https.

  Print