Rewrite to HTTPS

How do you do the rewrite so that whatever anyone puts in the browser address bar before the domain name ie, http://domain.com, www.domain.com etc, it will still go to https://www.domain.com please?

I think you’ll find that was asked and answered very recently:

Thanks, but this is for both www and non www

Also, how do you do the code so that it could relate to any website name? If for a wide example, you have 200 websites and you just wanted to copy and paste without keep changing the domain name.

Not sure what you mean by that. My understanding was that you wanted every other version of the URL to redirect to https://www.example.com, which is what the code in post 4 of that thread does.

I think he’s asking (in addition to that) for a *.example.com redirect.

2 Likes

Thanks - yep, that’s right.

Redirect anything to its HTTPS equivalent:

RewriteCond %{SERVER_PORT} ^80$
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

(try with R=302 while testing, then switch to R=301 once confirmed it works)

2 Likes

Thanks rpkamp, would the end result always be https://www.(domainname).com / co.uk ?

Given the URL http://(domain)/(path)?(query_string) the result would be https://(domain)/(path)?(query_string)

So yes, it would rewrite http to https, regardless of domain, path, or query string.

Thanks rpkamp, have just tried that code above, but it’s not redirecting always to https://www.

Ah, it wasn’t very clear that that was a requirement. Give this a whirl:

RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} !^www
RewriteRule . https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

RewriteCond %{SERVER_PORT} ^80$
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

Is it possible to combine those two into one?

Nope. They serve different purposes that can’t be rolled in to one. At least not in any sane way I can think of.