Redirect http:// to https:// and more


#21

thanks for sharing that tool :slight_smile:


#22

Thanks for the great post. I am using the following rewrite rule (without spaces). I am getting http redirected to https and non-www redirected to www. However, my problem is I am getting a 302 redirect for “https://website.com”. It is redirecting to https:// www. Any idea how to get this to a 301 or just not worry about it?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^website .com [NC]
RewriteRule ^(.)$ http://www. website .com/$1 [L,R=301,NC]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.
)$ https://%{HTTP_HOST}/$1 [R=301,L]


#23

Hi @jwebb189 this is what I do on cloudways that works for me:

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^domain\.com [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

For the last line add www. if you want it to go to https://www.domain.com

It returns 301 for me and correctly redirects all attempts to https://domain.com

You can see for yourself at the site I am using these rules for at https://myipiswhat.com that is on cloudways.


#24

Got it! Thanks very much!


#25

Hi, pff nothing worked and then this finally worked:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301,NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

This makes all the http to https-www and non-www to https-www.

Check it with https://varvy.com/tools/redirects/

Also make sure that whenever you change the htaccess file that you refresh the server and purge the varnish.


#26

I was struggling with this tonight, had to change my settings, and this finally did the trick for everything i need - including passing string variables in urls.


#27

you might also want to try the following set of generic directives – no specific domain name needed:

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

see Jeff Starr:
Redirect HTTP to HTTPS
Canonical www via .htaccess


#28

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