Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Forcing HTTPS with PHP

David Carr

PHP & MySQL Tutorials

    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();
    }

     

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.