Welcome to WebmasterWorld Guest from 54.225.32.164

Forum Moderators: Ocean10000 & phranque

Using 301 redirect to cover both www and https redirects

     
10:29 am on Jul 9, 2018 (gmt 0)

Preferred Member from GB 

5+ Year Member Top Contributors Of The Month

joined:Sept 29, 2009
posts:507
votes: 43


I am going to bite the bullet and change my site over to https in last weekend of July.

I already have the following doing a non-www-to-www redirect


# SET ALL URLS TO WWW VERSIONS
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


I can just add the 's' in there, but that wouldn't catch http-to-https, would it?

is it "http?s" I would need to do to catch both http and https?
1:35 pm on July 9, 2018 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:3834
votes: 212


No, all you need to change is add the s after http in the target URL so the last line becomes
RewriteRule (.*) https://www.example.com/$1 [R=301,L] 
That's how mine is and the switch went quite smoothly. My host suggested that I add one more rule and I did, but I don't see it as a "must have", they said it took care of scripts and external resources:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]


These lines should be after all other Rewrite Rules. (Unless you're using a CMS such as WP that has its own rules - then theirs comes after these rules).

6:12 pm on July 9, 2018 (gmt 0)

Preferred Member from GB 

5+ Year Member Top Contributors Of The Month

joined:Sept 29, 2009
posts:507
votes: 43


ah ok, so I don't need anything that covers both http and https in the first line, the RewriteCond line, because all eventualities are covered by %{HTTP_HOST} ?
6:33 pm on July 9, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14898
votes: 648


You need two RewriteCond, separated by OR:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [OR]
RewriteCond %{HTTPS} !on

A ? in the target has a completely different meaning than a ? in the pattern. Putting https?:// like that in the target will lead to a browser error when the rule deploys. (This is why we have test sites.)