Incorrect 301 Redirects Due to a Bug in “Redirect Canonical”

Update: I just created ticket #23602 on WordPress trac.

A bug in redirect_canonical function in /wp-include/canonical.php incorrectly redirects all requests for http://example.com/page/2?p=123 where 123 is a valid post ID to the home page. Here are few examples of sites that feature this bug:

I noticed this because of multiple soft 404 errors reported in Google Webmaster Tools:

Soft 404 errors in Google Webmaster Tools, WordPress canonical redirects

Judging by the time this was first detected by Google, this bug was likely introduced in WordPress 3.5.1 which was released on January 24, 2013. Looks like there haven’t been any changes to that file since September 2012.

A temporary fix would be to use the template_redirect filter and remove the p query variable if the request URI contains page/ and redirect to the archive page:

add_filter( 'redirect_canonical', 'fix_paged_redirects', 10, 2 );

function fix_paged_redirects( $redirect_url, $requested_url ) {
	if ( strpos( $requested_url, 'page/' ) !== false )
		return remove_query_arg( 'p', $requested_url );

	return $redirect_url;
}

Never lose a single business lead or enquiry again! Get my latest plugin Storage for Contact Form 7 now!

Buy now for only $19 →

One Comment

  1. Gary says:

    Thanks for highlighting this. I thought this was a problem with my Site alone. I’ll try the temporary fix.

Leave a Reply