Forcing HTTPS with PHP

David Carr

1 min read - 8th Mar, 2015

Using HTTPS is recomended, Google also preferes HTTPS they recently announced: “HTTPS is a ranking signal. For now it's only a very lightweight signal—affecting fewer than 1% of global queries, and carrying less weight than other signals such as high-quality content—while we give webmasters time to switch to HTTPS. But over time, we may decide to strengthen it, because we’d like to encourage all website owners to switch from HTTP to HTTPS to keep everyone safe on the web.”

Here is an easy to using PHP to force your website to use HTTPS instead of HTTP, of course you need an valid SSL certicate. 

if (! isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off' ) {
    $redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header("Location: $redirect_url");
    exit();
}

 

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.