Setting the canonical domain using hooks and filters.

#239851
  • Resolved Dan Antony
    Rank Math free

    Hello,

    It looks like RankMath uses the sites. “site_url” settings to generate the canonical URL.
    We run our website on a cloudflare reverse proxy so the site_url is set to “/blog”

    This makes our canonical be generated as (unknown) on the front end.
    I would like to set the domain name manually using hooks/filters or any other way.
    Can you tell us how this can be done and if there’s an example snippet for this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thanks for contacting us and sorry for any inconvenience that might have been caused due to that.

    You can modify and use the following filter in your theme’s functions.php file to set the canonical URL manually: https://rankmath.com/kb/filters-hooks-api-developer/#change-canonical-url

    You can also edit your homepage and add the canonical URL from the Advanced Tab of Rank Math’s meta box.

    Hope this helps. Let us know if you need any further assistance.

    Hello,

    We have an archive homepage. SO changing it individually will not make sense to us interms of effort.

    My exact question is I want to only change the domain name for canonicals in rankmath. Is that possible with the hook/filter you linked to or would I have to handle every post individually?

    Ankit
    Rank Math business

    Hello,

    You can use the below filter code to add the current page URL:

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

    I hope that helps. Thank you.

    Dan Antony
    Rank Math free

    Hi There,
    I modified the above hook to return the domain name that I want to set for
    canonical and set it up. It is working as I need it to now.

    Ankit
    Rank Math business

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘Setting the canonical domain using hooks and filters.’ is closed to new replies.