How to Redirect HTTP page to HTTPS in PHP

A white, rounded square logo with an abstract S-shaped design on a gradient blue and purple background.

Here you go!

<?
function secure_page()
{
if ( !isset($_SERVER[‘HTTPS’]) || strtolower($_SERVER[‘HTTPS’]) != ‘on’ )
{
header (‘Location: https://’.$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’]);
exit();
}
}
secure_page();
?>

Just put that at the top of your code. I recommend using an include file so that you only have to load one include at the top of your code (keeps it nice and clean).

Share This

Join the Sales and Marketing News, receive our last insights, tips and best practices.

Our 7 Guarantees

Keeping 2,000+ Clients Happy Since 2001.

1
You Will Love Your Design We design to please you and your clients
2
Same-Day Support 24-hour turnaround edits during business hours
3
Free Education We provide knowledge to help you expand
4
No Hidden Charges We quote flat-rate projects
5
Own Your Site No strings attached
6
We Create Results SEO, PPC, content + design = clients
7
We Make Life Easier One agency for web, branding and marketing