Blog

22 de June de 2023

Force HTTPS

Forçar o HTTPS

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();
		}
	}
?>
Share:
Others

Tips

Compactar arquivos com PHP

Compress files with PHP

View more

O que é Senha Forte?

What is Strong Password?

View more

Erro ao subir o projeto React

Erro ao subir o projeto React

View more