For your second question read this post from @Fox ,
maybe its what you need. Its in Russian, but you can
use translator. There is an example script too >
automatically redirects http to https
-
What URL are you talking about exactly?
-
The problem occurs e.g. on http://ip-api.com/json
The site automatically redirects from http to https.
But the way how the OP posted it seems to work.