Https

how can i make it that my wordpress site automatically uses https instead of http

Did you do this?

6 Likes

After you’re done that, follow this article.

7 Likes

You can use page script. Put it on top page:

<?php
function Redirect($url, $permanent = false)
{
    header('Location: ' . $url, true, $permanent ? 301 : 302);
    exit();
}

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $domainName = $_SERVER['HTTP_HOST'];
if ($protocol == "http://") {
        //echo "redirect.. please wait.";
        $new_location = "https://".$domainName.$_SERVER["REQUEST_URI"];
        Redirect($new_location); 
    } else {
        
    }
?>

No, you really should not use a PHP script for this.

And you code is wrong anyways, at it would redirect the users to the wrong location in some circumstances. It would also cause additional server resources to be used, so .htaccess is a much better choice.

3 Likes

Just put it on index.php or several main page, before html tag. So it will look like:

<?php
function Redirect($url, $permanent = false)
{
    header('Location: ' . $url, true, $permanent ? 301 : 302);
    exit();
}

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domainName = $_SERVER['HTTP_HOST'];
if ($protocol == "http://") {
        //echo "redirect.. please wait.";
        $new_location = "https://".$domainName.$_SERVER["REQUEST_URI"];
        Redirect($new_location); 
    } else {
        
    }
?>
<!DOCTYPE html>

I believe. it won’t redirect to something else.

Even if it would work, (I didn’t test it, but just by looking at it, I don’t think it would), PHP should not be used for that, .htaccess should be.

It’s kind of like using a spoon to eat long stringy noodles every single day, for every single meal. Sure, it’s possible, but using a fork would be way for efficient, and case way less annoyance.

.htaccess rules only have to be configured once, and they work on every page. The .htaccess rule for for forcing HTTP is only a few lines. And, .htaccess uses less server resources, which is almost a necessity on free hosting.

4 Likes

It would work. I already tested it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.