Last active
December 8, 2016 16:39
-
-
Save peterwegren/8982700 to your computer and use it in GitHub Desktop.
301s - WP Include File
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 | |
$url = get_home_url(); | |
// map redirects in array: [old url (path)] => [new url ($url + path)] | |
$redirect_urls = array( | |
// '' => $url . '', | |
); | |
/** | |
* page_redirects | |
* Custom 301 redirects. | |
* | |
* @method page_redirects | |
* | |
* @author Peter Wegren <peter @ paper-leaf.com> | |
* | |
* @return [null] | |
*/ | |
function page_redirects() { | |
global $redirect_urls; | |
$request = $_SERVER['REQUEST_URI']; | |
$request_explode = explode('&', $request, 2); | |
$request_trimmed = $request_explode[0]; | |
$request_remainder = array_key_exists(1, $request_explode) ? $request_explode[1] : false; | |
if ( $request_remainder ) { | |
$request_remainder = '&' . $request_remainder; | |
} | |
if ( array_key_exists( $request_trimmed, $redirect_urls ) ) { | |
$destination = $redirect_urls[ $request_trimmed ]; | |
header( 'Location: ' .$destination .$request_remainder, TRUE, 301 ); | |
exit; | |
} | |
unset( $redirect_urls ); | |
} | |
add_action('init', 'page_redirects'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment