Skip to content

Instantly share code, notes, and snippets.

@hellojebus
Created November 29, 2018 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellojebus/98e107ab1d116c3947dbb0a2aabc9f78 to your computer and use it in GitHub Desktop.
Save hellojebus/98e107ab1d116c3947dbb0a2aabc9f78 to your computer and use it in GitHub Desktop.
404 redirect checker
/*
* This piece of code checks to see if the 404ing URL exists under blog
* if so, redirects to the page, if not render normal 404 page
* */
$tmpUrl = "https://".$_SERVER["SERVER_NAME"]."/blog". $_SERVER["REQUEST_URI"];
//check for trailing slash
if(substr($tmpUrl, -1) == '/') {
$tmpUrl = substr($tmpUrl, 0, -1);
}
//get headers and http status code
$headers = get_headers($tmpUrl);
$statusCode = substr($headers[0], 9, 3);
//WP will return 301 on pages that exist, 302 on pages that don't
if ($statusCode == "301" || $statusCode == "200"){
Header("Location: ".$tmpUrl);
}
@hellojebus
Copy link
Author

refactor with curl instead of get_header

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment