Last active
February 4, 2016 02:13
-
-
Save Blindmikey/a5c3de31d085859715cb to your computer and use it in GitHub Desktop.
PHP Mass Redirection
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 | |
/** | |
* Simple PHP script for mass redirects | |
* | |
* @author: Michael Niles <github@blindmikey.com> | |
* @version: 1.0.0 | |
*/ | |
/** | |
* To Use, add this line to the top of your wp-config.php: | |
* require_once 'redirects.php'; | |
*/ | |
$new_site_domain = 'www.mynewsite.com'; | |
/** | |
* Array of redirects | |
* | |
* In the format of: {from} => {to} | |
* Where {to} is relative to yoursite.com | |
* And Where {from} is without http:// or https:// and without a trialing "/" | |
* | |
* @var array | |
*/ | |
$redirects = array ( | |
/** | |
* | |
* REDIRECT BLOCK | |
* | |
*/ | |
'www.somedomain.com' => '/welcome/', | |
); | |
/** | |
* | |
* Do not tread below - there be dragons | |
* | |
*/ | |
$current_url = rtrim($_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI], '/'); | |
// optional: | |
// catch sub.domain.ext/page/X and turn into sub.domain.ext | |
// else if catch sub.domain.ext/?cat=X and turn into sub.domain.ext | |
/* | |
$pagePattern = '/\/page\/[0-9]+/i'; | |
$catPattern = '/\/\?cat\=[0-9]+/i'; | |
if ( preg_match($pagePattern, $current_url, $match) ) | |
{ | |
$current_url = str_replace($match[0], '', $current_url); | |
} | |
else if ( preg_match($catPattern, $current_url, $match) ) | |
{ | |
$current_url = str_replace($match[0], '', $current_url); | |
} | |
*/ | |
// look into our redirect array and if we find a match, redirect | |
if ( ! empty($redirects[$current_url]) ) { | |
$redirect_to = $redirects[$current_url]; | |
header('Location: http'.(isSecure() ? 's' : '').'://'.$new_site_domain.$redirect_to, true, 301); | |
die(); | |
} | |
// make sure we serve https if needed | |
function isSecure() { | |
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') | |
|| $_SERVER['SERVER_PORT'] == 443; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment