How to redirect to HTTPS?

Asked

Viewed 1,061 times

0

I’m using the Cloudflare and my HTTPS URL’s are already working, but only if I write https://www.exemplo.com if I don’t write https, it keeps pointing to http. My hosting is Windows and I was informed by the same that could not use . htaccess to redirect because it only works on linux, any suggestions how to do when accessing http is automatically redirected to the address HTTPS?

  • 1

    If you are using Cloudflare, you can do this by the website’s own Adm

  • 1

    You can also use the Automatic HTTPS Rewrites

  • 1

    I managed to do by the Cloudflare website itself, THANK YOU VERY MUCH Randrade, thank you very much man, I didn’t think it could be so easy, hug!

  • 1

    Sometimes things are so simple that we don’t think to do it that way xD. I’m happy to help

2 answers

2


Put the script below in a file that is included by all pages:

if($_SERVER["HTTPS"] == "on")
{
    header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
    exit();
}

After that it will do the automatic redirect.

But if you wish you can do it yourself cloudflare.

2

<?php
function goHTTPS() {
    if ($_SERVER['HTTP'] == "on") {
        $url = $_SERVER['SERVER_NAME'];
        $new_url = "https://" . $url . $_SERVER['REQUEST_URI'];
        header("Location: $new_url");
        exit;
    }
}
goHTTPS();
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.