Make Default Canonical include trailing slash

#47618
  • Resolved Alan Martin
    Rank Math free

    Hi there,

    I’ve searched all over the knowledge base but cannot find a specific solution and my PHP skills are novice level. I would like the default canonical URLs for ALL post types to include a / at the end. I found this support article that is close but it is the opposite of what I’m looking for and I was unable to successfully tweak it to my needs. Any advice?

    https://support.rankmath.com/ticket/canonical-url-default/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Alberto
    Rank Math business

    Hello,

    Thank you for contacting Rank Math today.

    You can try the following custom filter to your theme’s functions.php file to modify the canonical URL:

    /**
     * Allow changing of the canonical URL.
     *
     * @param string $canonical The canonical URL.
     */
    add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
            $custom_url = RankMath\Post::get_meta( 'canonical_url', get_the_ID() );
    	if ( $custom_url ) {
                    if(substr($custom_url, -1)!="/"){
                         $canonical = $custom_url . "/";
                    }
    	}
            
    	return $canonical;
    });

    I hope this info helps. Thank you.

    Alan Martin
    Rank Math free

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Alan Martin
    Rank Math free

    Hi Alberto,

    Thanks for your help so far, however I added that PHP to the functions.php file and it did not change the canonical. Just so we are on the same page, I am looking for all default canonicals to include a trailing slash (/) at the end, as long as there is no custom canonical defined for a given page.

    Thoughts?

    Best,
    Alan

    Alberto
    Rank Math business

    Hello,

    You can try this code, it will check if a canonical is already defined in Rank Math and if it is, it uses it. And if it is not, it takes the current canonical and if it has not a slash at the end, it adds one:

    /**
     * Allow changing of the canonical URL.
     *
     * @param string $canonical The canonical URL.
     */
    add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
          $custom_url = RankMath\Post::get_meta( 'canonical_url', get_the_ID() );
    	if ($custom_url) {
           $canonical = $custom_url;
    	}else{
    		if(substr($canonical, -1)!="/"){
               $canonical = $canonical . "/";
             }
    	}
            
    	return $canonical;
    });

    Hope it works for you.

    Alan Martin
    Rank Math free

    Alberto,

    That last one did the trick. You rock! Thank you so much for the quick responses and follow-thru. RankMath FTW 🙂

    Best,
    Alan

    Alberto
    Rank Math business

    Hello Alan,

    I am super happy that this snippet 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.

    • This reply was modified 4 years ago by Alberto.
    David
    Rank Math free

    Hello,

    I’ve got the opposite issue.
    I would like that all default canonical URLs (products, pages…) to remove the slash “/” at the end.

    Why it doesn’t change by modifying permalink settings ?
    On Rank Math website it’s writen : “Even if you don’t set a canonical URL inside Rank Math, Rank Math will set the * post’s current URL* as the canonical URL of the page. Depending on your permalink settings, you might want to set a canonical URL yourself or change your permalink settings so that the default canonical URL is the one that works for you.”
    Is my unique chance to get it right was to have the good permalink setting before installing Rank Math ?

    If it’s not possible to solve this with permalink settings, I would need a code that will check if a canonical is already defined in Rank Math and if it is, it uses it. And if it is not, it takes the current canonical and if it has a slash at the end, it removes it.

    Hello David,

    I believe this thread: https://support.rankmath.com/ticket/canonical-url-default/ has the solution are looking for.

    Please check and let us know.

    Looking forward to helping you. Thank you.

    David
    Rank Math free

    Michael,

    Thank you but this code doesn’t work… There is still the trailing slash at the end.
    As asked by your colleague, I continue on the other thread : https://support.rankmath.com/ticket/make-default-canonical-remove-trailing-slash/

    • This reply was modified 3 years, 6 months ago by David.
    Alberto
    Rank Math business

    Hello,

    I will close this thread here and continue helping you in the other one.

    Looking forward to help you.

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

The ticket ‘Make Default Canonical include trailing slash’ is closed to new replies.