Skip to content

Instantly share code, notes, and snippets.

@thewheat
Created May 30, 2017 12:56
Show Gist options
  • Select an option

  • Save thewheat/37034b5e1c83106d44af92ba13da33d4 to your computer and use it in GitHub Desktop.

Select an option

Save thewheat/37034b5e1c83106d44af92ba13da33d4 to your computer and use it in GitHub Desktop.
Allows redirection of URLs from an old path to a new one
<?php
/*
#############################################################################################
# Manually handle URL redirections when transferring from an old custom domain to the new one
#############################################################################################
- This will allow you to manually redirect URLs if you move systems
- Likely not the best for performance reasons but is a workable solution
################
# Instructions #
################
1 )Get list of all links from old and new site. These commands will crawl through your sites and create links to all articles and save them to a text file
wget -nv -r old_site -o URLs_old.txt
wget -nv -r new_site -o URLs_new.txt
2) In the output text fields, manually find the correct mappings
3) Manually create PHP object of this format and save it in the 404.php file
$REDIRECT_URLS = [
"/old/url/path" => "/new-url/path",
];
4) Style this 404 page
*/
$REDIRECT_URLS = [
"/old/path/to/page" => "/new-page-to/page",
];
$url = @$REDIRECT_URLS[$_SERVER["REQUEST_URI"]];
if(!isset($url)) $url = @$REDIRECT_URLS[$_SERVER["REDIRECT_URL"]];
if(isset($url)){
header("Location: " . $url, TRUE, 301);
}
?>
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment