Redirect to https:// from non-SSL

Discussion in 'Site & Server Administration' started by jaguar34, Jun 15, 2020.

  1. #1
    What should be the code in .htaccess for redirecting to https version of website from Non-SSL? The website is html based
     
    Solved! View solution.
    jaguar34, Jun 15, 2020 IP
  2. #2
    
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 
    
    Code (markup):
     
    qwikad.com, Jun 15, 2020 IP
    dcristo, sarahk and jaguar34 like this.
  3. seomanualsubmission

    seomanualsubmission Well-Known Member

    Messages:
    913
    Likes Received:
    131
    Best Answers:
    4
    Trophy Points:
    165
    #3
    Yes this is perfect ..... You can add some more like ensure that site is opening with www or without www ...... permanently redirect from http to https etc. Take a look at below code with some additional parameters.

    Options +FollowSymLinks
    RewriteEngine on
    # ensure www. (If prefer without www then remove this section)
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # ensure https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    Code (markup):
     
    seomanualsubmission, Jun 15, 2020 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,271
    Likes Received:
    1,696
    Best Answers:
    31
    Trophy Points:
    475
    #4
    The greedy regex (.*) does that. It matches the URL with or without www. and/or http://

     
    qwikad.com, Jun 16, 2020 IP
  5. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #5
    Thanks, this worked well. But for SEO purposes you want to setup a 301 redirect.

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    Code (markup):
     
    dcristo, Nov 23, 2020 IP
    qwikad.com likes this.
  6. ElenaVital

    ElenaVital Greenhorn

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    5
    #6
    It would be more efficient to do the redirect in Cloudflare using their `Always Use HTTPS` and also mean less code for you to maintain. This is available on their free plan and you would also get ddos protection.
     
    ElenaVital, May 18, 2024 IP