Undoubtedly, the SSL certificate is one of the criteria that guarantees the security of the visitor on a website, allowing access via the HTTPS protocol, however, nothing prevents the visitor from accessing via HTTP. I'm going to show you how to force a redirect to HTTPS through PHP code. Let's go there?
To do this, it's simple, just put the code at the top of the site:
<?php
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
if(!headers_sent()) {
header("Status: 301 Moved Permanently");
header(sprintf(
'Location: https://%s%s',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
));
exit();
}
}
?>