Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress redirect 404 with valid slug
function q11_redirect_uri_site_antigo() {
if ( is_404() ) {
$url = 'INSERT_YOUR_DOMAIN_HERE' . $_SERVER['REQUEST_URI'];
$end = end(explode('/', rtrim($url, '/')));
if ($end) {
if ( $post = get_page_by_path( $end, OBJECT, array('post', 'page'))) {
$id = $post->ID;
} else {
$id = 0;
}
if ($id !== 0) {
$url_redir = get_permalink($id) . '?utm_source=page_404&utm_campaign=auto_redirect';
wp_safe_redirect( $url_redir, '301' );
exit;
}
}
}
}
add_action( 'template_redirect', 'q11_redirect_uri_site_antigo' );
@jemoreto

This comment has been minimized.

Copy link
Owner Author

@jemoreto jemoreto commented Sep 1, 2017

Useful if you changed your WordPress installation and/or your permalink structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment