Skip to content

Instantly share code, notes, and snippets.

@RiodeJaneiroo
Last active June 11, 2020 13:40
Show Gist options
  • Save RiodeJaneiroo/ce8e90f636af7c250256915e248a94ec to your computer and use it in GitHub Desktop.
Save RiodeJaneiroo/ce8e90f636af7c250256915e248a94ec to your computer and use it in GitHub Desktop.
[Wordpress SEO redirect from any symbol ] 301 redirect from wrong url #seo #yoast #wordpress #redirect
function wrong_url_redirect($url) {
global $wp;
if(!get_query_var('paged') && !empty($url) && !is_front_page() && $url != home_url($wp->request . '/' ) && !is_search()) {
wp_safe_redirect( $url, 301 );
exit;
}
return $url;
}
add_filter( 'wpseo_canonical', 'wrong_url_redirect' );
add_action( 'init', function() {
remove_filter( 'template_redirect', 'redirect_canonical' );
} );
function wrong_url_redirect($url) {
if(is_page() || is_single() || is_tax() || is_archive() ) {
global $wp;
if(!get_query_var('paged') && !empty($url) && empty($_GET) && !is_front_page() && $url != home_url($wp->request) && !is_search()) {
wp_safe_redirect( $url, 301 );
exit;
}
}
return $url;
}
add_filter( 'wpseo_canonical', 'wrong_url_redirect' );
function wrong_url_redirect() {
if(is_page() || is_single() || is_tax() || is_archive() ) {
global $wp;
$url = get_the_permalink(87);
if(!get_query_var('paged') && !empty($url) && empty($_GET) && !is_front_page() && $url != (home_url($wp->request).'/') && !is_search()) {
wp_redirect( $url, 301 );
exit;
}
}
}
add_filter( 'template_redirect', 'wrong_url_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment