8

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?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
Falch0n
  • 305
  • 1
  • 3
  • 11
  • Possible duplicate of [apache redirect http to https and www to non www](http://stackoverflow.com/questions/9945655/apache-redirect-http-to-https-and-www-to-non-www) – Simone Carletti Jun 28 '16 at 08:02

2 Answers2

20
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

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • 1
    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) – Bombelman May 26 '18 at 17:25
  • 2
    @Bombelman Sorry, that was a typo. I have updated my answer with `R=301,NE` flags. Thanks for your comment. Cheers! – Amit Verma May 27 '18 at 05:05
3

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]
Joe
  • 4,877
  • 5
  • 30
  • 51
Sachin Singh
  • 113
  • 1
  • 7