I have 1300+ pages that have 'non-canonical pages in sitemap' issue based on Ahrefs. All of the canonical URLs are DNS URL like this.

Canonical URL screenshot

I would like to set the canonical URL to be the same as the URL's page. Is there any method that I could try? Thank you.

P.S. I'm using RankMath for the SEO tool.

1 Answer 1


In Rank Math, you can set the canonical URL on a page-by-page basis. In /wp-admin/ go to either a Page or Post -> Edit Page/Post -> scroll down to RankMath SEO -> click the Advanced tab.

Add the canonical URL of your choice in the field like so:

rank math seo setting the canonical url for a page or post

RankMath, by default, tries to automagically set canonicals for post/pages. Hopefully, that overrides the annotation shown in your screenshot where you've got a CDN relative path. I'm guessing that for some reason RankMath is catching URIs from your CDN, and without seeing one specifically set, its automagical process is setting those as the canonical.

Controlling RankMath Canonical URLs via Functions.php

If that doesn't work, or if you want to be able to modify the canonical dynamically, set a filter in your functions.php like so:

// Allow changing of the canonical URL. 
//@param string $canonical The canonical URL.
add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
    return $canonical;

For example, this would set to the current page URL:

add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
    return home_url( $_SERVER['REQUEST_URI'] );

If after attempting all of the above RankMath is still setting CDN relative paths as the canonical URL, the issue is likely specific to your site (which would be off-topic for us to answer here). In such case, you'd want to take this up with your hosting provider and/or CDN customer service.

Quick Side Note About Sitemaps

Also, just an FYI - Google assumes URLs submitted in a sitemap to be canonical. So best practice in general is to always leave non-canonical URLs off of your sitemaps. Unless something has changed, I'm fairly certain that's how it works. I'll look for someone to validate that in the comments.

  • Hi Mike! Thanks so much for the brief explanation. I have tried both the methods above, but it still displayed like this: monosnap.com/file/0CgqPbdL7fy93bEShM8kVLVXKqEZc6 Did I add the filter incorrectly? Or does the issue is related to the server or CDB afterall? Thanks again.
    – Arinda
    Dec 29, 2021 at 7:54
  • No problem, happy to help. I dunno if you've added it correctly, I can't see your implementation!😅 Dec 30, 2021 at 6:15
  • I predict that this is a hosting/cdn customer service type of thing Dec 30, 2021 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.