Redirect HTTPS to HTTP using PHP

Sometimes in the file .htaccess redirect HTTPS to HTTP does not work. In this case, you can do a redirect using PHP.
The bottom code should be inserted into the top of a page, before the document output and functions «echo».

Code for redirection in PHP:

if((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]!='off') || 
(isset($_SERVER["HTTP_X_HTTPS"]) && $_SERVER["HTTP_X_HTTPS"]=='1'))
	$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	header('HTTP/1.1 301 Moved Permanently');
	header('Location: '.$redirect_url);

Please note that the redirect gives a 301 response status. This status indicates that the page is moved to new address permanently.