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.

No votes yet.
Please wait...

About the author

MillaN
MillaN
Dev4Press owner and lead developer

Programmer since the age of 12 and now WordPress developer with more than 8 years of WordPress experience, author of more than 100 plugins and more than 20 themes.

3 Comments

  1. Nabtron says:

    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/

    0
    Be the first one to like this.
    Please wait...
  2. Steve says:

    Man, thank you very much. You helped me greatly.

    0
    Be the first one to like this.
    Please wait...
  3. pamela sillah says:

    I added this to my function.php but im still getting redirected to my index.php page.

    0
    Be the first one to like this.
    Please wait...

Leave a Reply

Your email address will not be published. Required fields are marked *