.htaccess – Redirect to https

.htaccesshttpsredirect

I'm using the following .htaccess to redirect example.com or www.example.com to http://www.example.com.

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

If the address is example.com/login I would to redirect to https instead of http. How to do that?

Thank you.

Best Answer

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

# check if https if off and check if the requested uri ends with login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} login$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Related Topic