Redirect non-www and non-http to https

12,891

Solution 1

RewriteEngine on


RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R=301,NE]

This will redirect both http or non-www to https://www

Solution 2

Use:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

OR you can try:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Share:
12,891
Falch0n
Author by

Falch0n

Interaction Designer at Ssenz

Updated on July 28, 2022

Comments

  • Falch0n
    Falch0n over 1 year

    Yesterday I Installed SSL on the server. Since than I can't reach some pages.

    www.example.com/amsterdam/shoes

    example.com/amsterdam/

    ^ both do not redirect to https://, not even http://

    www.example.com/amsterdam

    ^ does redirect to https://

    How do I redirect all pages to HTTPS with www via .htaccess?

  • Bombelman
    Bombelman over 5 years
    thanks, this works for me. do you have a reason for not using R=301 or NE ? (I've seen this a lot with other redirects)
  • Amit Verma
    Amit Verma over 5 years
    @Bombelman Sorry, that was a typo. I have updated my answer with R=301,NE flags. Thanks for your comment. Cheers!