Canonical Header on CDN


#1

Should we enable “Canonical Header” on CDN?


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

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.


#3

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 https://images.google.com/

  3. You will see new images also indexed.


#4

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


#5

Keycdn recommends adding canonical to your cdn


#6

Just read moz



#7

Also read this: https://webmasters.googleblog.com/2011/06/supporting-relcanonical-http-headers.html


#8

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

Ref: https://support.google.com/webmasters/answer/139066?hl=en


#9

Do Cloudflare has this setting? @gulshankumar


#10

Cloudflare don’t provide rel canonical header.


#11

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:


#12

How do we include CDN image URL in the sitemap?


#13

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.


#14

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( 'https://www.example.com', 'https://cdn.example.com', $uri );
}
add_filter( 'wpseo_xml_sitemap_img_src', 'wpseo_cdn_filter' );

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


#15

Done! Now how should I confirm that this worked?


#16

Check view source of the sitemap: view-source:https://www.example.com/post-sitemap.xml


#17

It works flawlessly. Thanks :slight_smile:


#18

You’re welcome :grinning:


#19

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


#20

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: