skip to Main Content

Hello I want to redirect my http requests to https. I add some code to .htaccess but still I can access to my website with http. What’s wrong and what should I do?

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]

</IfModule>
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteOptions inherit
Header set content-Security-Policy: upgrade-insecure-requests

2

Answers


  1. With your shown attempts, could you please try following htaccess rues set. Since you are using inherit option for Rules, so in case you want to apply https in all URLs then better put your htpps rules in your BASE(very first level folder wise) htaccess file and we need not to put them here, if you want to only apply https to URLs which are covered here then use following style rules.

    Please make sure to clear your browser cache before testing your URLs.

    <IfModule mod_rewrite.c>
    
      RewriteOptions inherit
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
      
      RewriteBase /
      RewriteRule ^index.html$ - [L]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-l
      RewriteRule . /index.html [L]
    
    </IfModule>
    
    Header set content-Security-Policy: upgrade-insecure-requests
    
    Login or Signup to reply.
  2. The following code redirect any URL of your domain to HTTPS version and set HSTS:

    #Redirect HTTP a HTTPS
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    #HSTS
    <if "%{HTTPS} == 'on'">
      Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
    </if>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search