Skip to content

Instantly share code, notes, and snippets.

@peterwegren
Last active December 8, 2016 16:39
Show Gist options
  • Save peterwegren/8982700 to your computer and use it in GitHub Desktop.
Save peterwegren/8982700 to your computer and use it in GitHub Desktop.
301s - WP Include File
<?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