Skip to content

Instantly share code, notes, and snippets.

@netconstructor
Forked from norcross/head_redirect.php
Created May 1, 2012 10:42
Show Gist options
  • Save netconstructor/2567157 to your computer and use it in GitHub Desktop.
Save netconstructor/2567157 to your computer and use it in GitHub Desktop.
PHP redirects
<?php
add_action ('wp_head', 'rkv_new_redirects', 1); // create redirects
function rkv_new_redirects() {
$newurl = 'http://mynewurl.com';
$slug = basename(get_permalink());
$cat = get_query_var('cat');
$cat_s = get_category ($cat);
$c_slug = $cat_s->slug;
// set up
if (is_home() || is_front_page() ) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$newurl.'/');
}
elseif (is_category() ) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$newurl.'/category/'.$c_slug.'/');
}
elseif (is_page() ) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$newurl.'/'.$slug.'/');
}
elseif (is_singular('post') ) { // may take some tweaking depending on site URL structure
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$newurl.'/'.$slug.'/');
}
else {
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$newurl.'/');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment