Redirecting pages to HTTPS with htaccess

Once you have installed your SSL certificate on your website you need to look at how you are going to ensure visitors use HTTPS on the correct pages.

The easiest method of doing this is via htaccess. If you are running on a Windows Server you can use ISAPI_Rewrite which adds support for htaccess to your server.

The htaccess will need to contain the following in all cases

RewriteEngine On

To simply redirect your entire website to HTTPS add the following to your htaccess

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

If you only want to redirect certain folders or pages to HTTPS  then use the following

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^(list|of|folders|pages|use|pipes|for|multiples)/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

To ensure pages you do not want as HTTPS are redirected to HTTP use the following

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^(list|of|folders|pages|that|should|be|HTTPS)/?.*$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  • ssl, https, htaccess
  • 30 Users Found This Useful
Was this answer helpful?