Rewrite to HTTPS

#1

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?

#2

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

#3

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.

#4

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.

#5

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

2 Likes
#6

Thanks - yep, that’s right.

#7

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
#8

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

#9

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.

#11

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

#12

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]
#13

Is it possible to combine those two into one?

#14

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

closed #18