Blog Post

Canonical Redirect: Problem and Solutions

You have noticed how WordPress can fix and redirect incorrect and incomplete URL’s to your blog, and how different way you can link content will always redirect to one based on permalinks settings? That is called canonical redirect, and it is part of WordPress since version 2.3.

So, if you have permalinks structure for posts set to /%year%/%postname%/, and you try to reach post by its id with URL query argument, WordPress will resolve the request, and then redirect to permalinks based URL. So, if you have posted with ID 10, and permalinks URL for it is something like this:

http://www.example.com/2012/my-post-is-here/

And you try to load the page with this:

http://www.example.com/?p=10

You will see that URL is redirected in the browser to main permalinks based URL.

And this is a really great feature. Even when you make a small mistake in the URL, there is a chance that canonical redirect will deal with it and show you what you were looking for. The good thing about this is that you will have only one valid URL to a page or a post, so search engines will not index the same page with different URL. And nowadays, you can get the penalty from search engines with duplicated content. So, the canonical redirect is a very good thing to have.

But, there are cases where this is actually a bad thing, and you don’t want URL to be modified by WordPress like this. Some website owner runs some third-party plugins that deal with canonical URL’s, or they simply don’t want it on. And, as with so many WordPress features you simply don’t have an option to turn it off. There is a simple one line code you can add to your theme functions.php or some other place to disable canonical redirect from loading:

remove_filter('template_redirect', 'redirect_canonical');

But, what if you want canonical redirect working, but not for every URL? Well, you can do that also, and it is not that complicated to do. What can be the reason for this? While developing GD Products Center plugin, I needed a very important feature: each post needs to have 5 pages, and I didn’t want numerical values for them, I wanted each page to have own URL. So, if the main post URL was something like this:

http://www.example.com/products/phone/samsung-galaxy-s2/

My added URL’s for it look like this:

http://www.example.com/products/phone/samsung-galaxy-s2/media/
http://www.example.com/products/phone/samsung-galaxy-s2/review/
http://www.example.com/products/phone/samsung-galaxy-s2/related/

The plugin was made to handle this extra element of the URL, and display proper content. But, canonical URL was not letting such URL through and it was always redirecting back to basic post URL. The only way to handle this, without disabling canonical redirect, is to attach a filter into that redirect function and prevent a change in some cases. The filter you need for this is redirect_canonical.

So, the example for this would be:

add_filter('redirect_canonical', 'my_redirect_canonical', 10, 2);
function my_redirect_canonical($redirect_url, $requested_url) {
  if (is_singular('phone') {
    return $requested_url;
  } else {
    return $redirect_url;
  }
}

Filter parameter $redirect_url is canonical URL created by the canonical redirect function and $requested_url is originally requested URL. Within my_redirect_canonical function we need to decide if we want canonical URL or if we want to leave the original URL intact. This example here shows that we are checking if the post type for the request is for ‘phone’ custom post type. If it is, it will use original URL, if not it will redirect to canonical. You can change this to use different methods to check what URL to use.

So these are two methods to use to fully or partly disable canonical redirects. In most cases, you need to leave this redirect method active, but it is good to know how to handle it if you need it gone, or at least partly disabled.

Please wait...

About the author

Milan Petrovic
Milan Petrovic

CEO and Lead developer of Dev4Press Web Development company, working with WordPress since 2008, first as a freelancer, later founding own development company. Author of more than 250 plugins and more than 20 themes.

Subscribe to Dev4Press Newsletter

Get the latest announcements, release digests, promotions and exclusive discounts, and general Dev4Press-related news straight into your mailbox.


This form collects your email (optionally your name) for the purpose of sending you newsletters. Check out our Privacy Policy for more information on how we store and manage your data. We will not send you any spam. Newsletters are sent 2 to 4 times every month.

4 thoughts on “Canonical Redirect: Problem and Solutions”

  1. I’ve been trying to find manual code to add seo optimized canonicals to my wp, coz plugins are just too buggy and have features which i don’t want!

    finally came up with this piece of code for functions.php!

    http://nabtron.com/manually-canonical-wordpress-permalinks-without-plugins/8177/

    Please wait...
    Reply
  2. Man, thank you very much. You helped me greatly.

    Please wait...
    Reply
  3. I added this to my function.php but im still getting redirected to my index.php page.

    Please wait...
    Reply
  4. Thanks! This information is what I’ve been looking for!

    Please wait...
    Reply

Leave a Comment

WP Rocket - Make WordPress Load Fast in a Few Clicks
SiteGround - Managed WordPress Hosting