---
title: Incorrect 301 Redirects Due to a Bug in "Redirect Canonical"
date: 2013-02-24T11:48:01+00:00
modified: 2013-02-25T08:35:50+00:00
image:: https://kaspars.net/wp-content/uploads/2013/02/google-webmaster-tools-reporting-wordpress-soft-404.png
permalink: https://kaspars.net/blog/bug-redirect-canonical-301
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
category:
  - WordPress
---

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

**Update**: I just created [ticket #23602](http://core.trac.wordpress.org/ticket/23602) on WordPress trac.

A bug in `redirect_canonical` function in `<a href="http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/canonical.php#L39">/wp-include/canonical.php</a>` 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:

- [ma.tt/page/2/?p=42100](http://ma.tt/page/2/?p=42100)
- [yoast.com/page/1/?p=61478](http://yoast.com/page/1/?p=61478)
- [techcrunch.com/page/2/?p=755740](http://techcrunch.com/page/2/?p=755740)
- All WordPress.com and WordPress VIP sites!

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

[![Soft 404 errors in Google Webmaster Tools, WordPress canonical redirects](https://kaspars.net/wp-content/uploads/2013/02/google-webmaster-tools-reporting-wordpress-soft-404.png?strip=all&quality=90&resize=500,270)](https://kaspars.net/wp-content/uploads/2013/02/google-webmaster-tools-reporting-wordpress-soft-404.png)

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](http://wordpress.org/news/2013/01/wordpress-3-5-1/). 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;
}
```