Skip to content

Instantly share code, notes, and snippets.

@norcross
Created April 29, 2012 22:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save norcross/2553507 to your computer and use it in GitHub Desktop.
Save norcross/2553507 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