How to redirect website from HTTP to HTTPS only with PHP
Posted: Wed Oct 19, 2022 1:11 pm
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:
With WordPress you can simply add this at beginning of the wp-config.php file
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;
}