Last active
December 17, 2015 14:19
-
-
Save CodeAngry/5623806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The right way to do a SEO HTTP Redirection. (says me) | |
* | |
* @copyright Claude "CodeAngry" Adrian | |
* @link http://codeangry.com/ | |
* @license WTFPL | |
*/ | |
namespace CA; | |
/** | |
* Redirection is permanent, resource moved. | |
*/ | |
const HttpRedirect_Permanent = 301; | |
/** | |
* Redirection is temporary, resource still lives here. | |
*/ | |
const HttpRedirect_Temporary = 302; | |
/** | |
* The right way to do a Redirection (SEO friendly and such). | |
* If returns false then you can't code! | |
* | |
* @param string $URL | |
* @param int $StatusCode | |
* @return bool | |
*/ | |
function HttpRedirect($URL, $StatusCode = 302){ | |
//<ARGS:VALIDATE> | |
// Validate URL | |
if(empty($URL)){ | |
$URL = $_SERVER['REQUEST_URI']; // Empty falls back to current | |
} | |
if(!is_string($URL) or !strlen($URL = trim($URL))){ | |
trigger_error('$URL has to be a proper string, you non-coder.', E_USER_WARNING); | |
return false; // So bad it hurts! | |
} | |
// Validate Status Code | |
if(empty($StatusCode)){ | |
$StatusCode = 302; | |
} | |
if(!is_numeric($StatusCode) or (intval(($StatusCode = intval($StatusCode)) / 100) != 3)){ | |
trigger_error('HTTP $StatusCode must be in 3##, you non-coder.', E_USER_WARNING); | |
return false; | |
} | |
//</ARGS:VALIDATE> | |
// <PLAN:A> | |
// Do http redirect if headers are not sent | |
if(!headers_sent()){ | |
header("Status: {$StatusCode} Relocated"); | |
header("{$_SERVER['SERVER_PROTOCOL']} {$StatusCode} Relocated"); | |
header("Location: {$URL}", $StatusCode); | |
} | |
// </PLAN:A> | |
// <PLAN:B> - Truth is one should NOT get here! | |
$URL_Escaped = htmlentities($URL); // For HTML tag output | |
if($StatusCode == 301){ | |
// Do a rel=canonical if the HTTP 301 failed due to sent headers (similar effect for crawlers) | |
echo '<link rel="canonical" href="', $URL_Escaped, '"/>', PHP_EOL; | |
} | |
// Try to redirect with JS | |
echo '<script type="text/javascript">', | |
'window.location.href="', $URL, '";', | |
'</script>', PHP_EOL; | |
// Try to redirect Meta Redirect | |
echo '<meta http-equiv="refresh" content="0;url=', $URL_Escaped, '" />', PHP_EOL; | |
// </PLAN:B> | |
// <PLAN:C> - Truth is one should NEVER get here! | |
// Try to redirect with User | |
echo '<div><a href="', $URL_Escaped, '">', $URL_Escaped, '</a></div>'; | |
// </PLAN:C> | |
return true; | |
} | |
/** | |
* Alias of HttpRedirect($URL, 302);. | |
* | |
* @param string $URL | |
* @return bool | |
*/ | |
function HttpRedirected($URL){ | |
return HttpRedirect($URL, 302); | |
} | |
/** | |
* Alias of HttpRedirect($URL, 301);. | |
* | |
* @param string $URL | |
* @return bool | |
*/ | |
function HttpMoved($URL){ | |
return HttpRedirect($URL, 301); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment