Page 1 of 1

How to redirect website from HTTP to HTTPS only with PHP

Posted: Wed Oct 19, 2022 1:11 pm
by isscbta
In a scenario where you need to force the domain to use only HTTPS, but the only way to do that is through PHP, use this piece of code:

Code: Select all

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
    $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $location);
    exit;
}
With WordPress you can simply add this at beginning of the wp-config.php file