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]


#29

This seems to be the best one since it’s a single rule and doesn’t require and hard-coding of the domain.


#30

Add this to the htaccess file and replace the word example with YOUR DOMAIN NAME.

This will redirect all http:// to https://. This will also redirect all non-existent subdomain to the homepage. (dne.example.com to example.com).

  • For instance, if a user types an incorrect subdomain it redirects to the homepage.
  • This only works for single site Wordpress not WPMU.
  • It works with CDNs such as CloudFlare, etc.

Option 1: Root Domain with WWW in front (www.example.com)

# ENSURE WWW ON MAIN PAGE
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ENSURE HTTPS EVERYWHERE
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# REDIRECT NOT IN USE SUBDOMAINS TO ROOT DOMAIN
RewriteCond %{HTTP_HOST} !^(example\.com$|www\.example\.com$) [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301,QSA]

Option 2: Root Domain without WWW in front (example.com)

# ENSURE HTTPS EVERYWHERE
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# REDIRECT NOT IN USE SUBDOMAINS TO ROOT DOMAIN
RewriteCond %{HTTP_HOST} !^(example\.com$|www\.example\.com$) [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301,QSA]

Final Step: Database http:// to https://

On Cloudways Server, go to Access Details tab and Launch Database Manager. Go to SQL Commands located on the Left Sidebar.

  • If you are using WWW make sure to add http://www and https://www !!!
  • Replace the word example with YOUR DOMAIN NAME! Then Execute.

UPDATE wp_options SET option_value = replace(option_value, 'http://example.com', 'https://example.com') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://example.com','https://example.com');

UPDATE wp_posts SET post_content = replace(post_content, 'http://example.com', 'https://example.com');

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://example.com', 'https://example.com');