0
votes

I'm trying to make my url without www redirect to www and my url without https redirect to https in the minimum of possible redirects.

I'm using this rule

# BEGIN SSL Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# END SSL Redirect

the problem is that this rule generates many redirects if I try to access this url

example.com

do it

http://example.com/ ==> http://www.example.com/ ==> https://www.example.com/

I would like you to do this

http://example.com/ ==> https://www.example.com/

1

1 Answers

0
votes

You can use this :

# BEGIN SSL + non www to www  Redirect
<IfModule mod_rewrite.c>
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]
</IfModule>
# END SSL + www Redirect

This will redirect both non-ssl and non-www to https://www in a single request.

If you are not on a proxy proxy server (ie : cloudflare..) then you will need to replace this line RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR] with RewriteCond %{HTTPS} off [OR] in order for this to work.