Canonical Header on CDN


Should we enable “Canonical Header” on CDN?

BunnyCDN - Review of Lightning fast CDN
[Guide] How to Setup W3 Total Cache Plugin?

I believe you should. I enabled it a few days ago as well on BunnyCDN. Think of it as a good way to canonicalize your media. The image is created on the source and served over the CDN, which means two different URLs for the same file.


Fact 1: We use CDN for static content that includes primarily Images which Google index in the SERPs.

Fact 2. But, Google doesn’t support canonical for images. Ref 1: Search Engine Roundtable
So, using canononical at CDN doesn’t matter.

What we should do if we shift Images to CDN?

  • We should update CDN path for images in the Sitemap.

That’s all.

After a week

  1. Next, keep eye on Search Console > Crawl > Sitemap > Images Index count

  2. Open

  3. You will see new images also indexed.


What if We enabled Canonical Header then the CDN images will not be indexed?


Keycdn recommends adding canonical to your cdn


Just read moz


Also read this:


This is why, we shoudn’t use canonical header for images as it comes under Image result.



Do Cloudflare has this setting? @GulshanKumar


Cloudflare don’t provide rel canonical header.


Thanks @GulshanKumar, I checked top site such as Yoast and Wpbeginner and found they also don’t use the rel canonical header on CDN. So in 2018, there is no need to use such header if we include cdn image url into the sitemap. :smile:


How do we include CDN image URL in the sitemap?


You’re welcome!

Link canonical header basically got popularity on Internet because of PDF content and CDN integration issue. Google never cared it for images. It was always for Web Results. They index images what they see at any webpage and Sitemap. Now even it is more difficult to analyse what image URL Google is indexing. As they have stopped showing download option.


If you’re using Yoast for generating sitemap then add the following lines in the function.php file:

function wpseo_cdn_filter( $uri ) {
return str_replace( '', '', $uri );
add_filter( 'wpseo_xml_sitemap_img_src', 'wpseo_cdn_filter' );

Don’t forget to replace to your domain name otherwise your site may brake. Please note I have tested the code with genesis based theme.


Done! Now how should I confirm that this worked?


Check view source of the sitemap: view-source:


It works flawlessly. Thanks :slight_smile:


You’re welcome :grinning:


This snippet is really being helpful when sometime changes in image path doesn’t reflect automatically. Then this manual approach work.


I got it a little wrong here. After seeing @GulshanKumar 's reply I reverted the code snippet and cleared the cache. I observed that CDN image URLs were already included in sitemap. So, it works automatically in my case.

But that’s really helpful for those who are struggling to do this manually. Saved it for future reference. :wink: