Skip to content

Instantly share code, notes, and snippets.

@jeremyrwross
Created March 13, 2015 20:39
Show Gist options
  • Save jeremyrwross/70c872661b7e6b55bfad to your computer and use it in GitHub Desktop.
Save jeremyrwross/70c872661b7e6b55bfad to your computer and use it in GitHub Desktop.
Wordpress 301 redirect using functions.php
add_action('template_redirect','my_template_redirect');
function my_template_redirect() {
$redirect_to = false;
list($url_path,$params) = explode('?',$_SERVER['REQUEST_URI']);
$path_parts = explode('/',trim($url_path,'/'));
switch ($path_parts[0]) {
case 'wrongpage1.html':
$redirect_to = '/';
break;
case 'wrongpage2.php':
$redirect_to = '/new-page/';
break;
case 'wrongpage.htm':
$redirect_to = '/news';
break;
}
if ($redirect_to) {
wp_safe_redirect($redirect_to,301);
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment