How to Have Yoast SEO’s XML Sitemap Register Images Served from a Content Delivery Network (CDN)

If you’re serving your site’s images from a CDN, this snippet tells Yoast SEO to register the CDN versions in its sitemap.

Yoast SEO logo
Last Updated:

This post may include affiliate links. Read more.

If you’re having trouble getting your images indexed by Google, and you’re using the combination of a CDN (content delivery network) and Yoast SEO on WordPress, it might be because Yoast SEO’s sitemap is pointing to the non-CDN versions, while Google can only see the CDN versions mentioned on your site.

But it’s very easy to fix. Add this snippet to your functions.php file, replacing the MYDOMAIN.com and CDN.MYDOMAIN.com sections with whatever matches your setup.

// Telling Yoast XML Sitemap to redirect images to CDN versions
function wpseo_cdn_filter( $uri ) {
return str_replace( '//MYDOMAIN.com', '//CDN.MYDOMAIN.com', $uri );
}
add_filter( 'wpseo_xml_sitemap_img_src', 'wpseo_cdn_filter' );

When the XML sitemap is regenerated, it should now point the image files to the CDN versions.

It then can take a while for Google to pick up on the change and start indexing your images–it doesn’t usually happen overnight.

Extra Tip: Add CDN to Search Console

Google also recommends that you add the CDN’s subdomain to your Search Console properties so that they can alert you to any crawling errors they come across there.

Things Worth Knowing

If something goes wrong with the functions.php file, it can create a white screen for your website. So it’s a good idea to back up your functions.php before you start so that you have a working copy to quickly restore. Restoring the backup version should get the site back up immediately.

David Coleman / Photographer

David Coleman

I'm a professional freelance travel photographer based in Washington DC. Seven continents, up mountains, underwater, and a bunch of places in between. My images have appeared in numerous publications, and you can check out some of my travel photography here. More »

Leave a Comment