Fariz Luqman Fariz Luqman - 3 months ago 33
Apache Configuration Question

Redirect http://www.domain.com to https://www.domain.com using .htaccess

I want to redirect my domain from http to https:

http://www.domain.com --> https://www.domain.com


Is it possible? I've been searching around the internet but I only found:

http://domain.com --> https://www.domain.com


The question is, how about peoples arriving directly on http://www.domain.com? Aren't they be served with non-https url? Same as the vice versa. I just want a simple redirection from the HTTP to HTTPS. Is it possible?

Thank you

Answer

It will work for ALL versions of php and will force SSL and www to all naked domains and links:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
  • We DO need the NE
  • www. is optional in 4th line
  • "off" is required to be.