简体   繁体   中英

http:// to https://www redirect not working

I want to redirect http:// to https://www . Now it is redirecting https:// then https://www

I am using siteground I tried many answers given on StackOverflow.

Example of codes I tried:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
<IfModule mod_rewrite.c>
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

You were close you need not to use OR condition after checking https condition, could you please try following. Following rules should apply https to a non http url with/without www .

Please make sure you clear your browser cache before you test your URLs.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

If you are using Cloudflare CDN or any other proxy server then you will need to match against %{HTTP:X-Forwarded-Proto} variable to detect the URL scheme. You can use the following rule that redirects http and https-noWWW to https://www in a single request:

 RewriteEngine on

RewriteCond %{HTTP:X-Forwarded-Proto} http$ [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]

Make sure you clear your browser cache before testing this rule.

This is the perfect redirection

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

the redirect rules mentioned here is perfect but will work only if you disable server-side redirect.

In your siteground hosting account Goto site tools > Security > HTTPS enforce and disable it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM