Reputation: 17
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>
Upvotes: 1
Views: 1626
Reputation: 17
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
Upvotes: 0
Reputation: 41249
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.
Upvotes: 1
Reputation: 133760
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]
Upvotes: 3