Setting or clearing canonical tag

#796518
  • Resolved Anonymous
    Rank Math free

    I am trying to remove canonical tag on some custom dynamic pages with remove_action('wp_head', 'rel_canonical') or setting the canonical tag that I need with add_filter( 'get_canonical_url', 'custom_set_canonical_url', 10, 2 ). None is working. Is it possible the plugin is preventing it from working?
    I tried using a filter I found in your website:

    add_filter('rank_math/frontend/canonical', function ($canonical) {
    global $wp;
    return home_url($wp->request);
    });

    But still, the pages are showing the canonical to the original page url. But these pages are dynamic and are accessed via multiple URLs and each one is for different content.

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

    I apologize for the issue you are facing with setting or clearing the canonical tag on your custom dynamic pages. It seems like you are trying to manipulate the canonical tag using WordPress hooks and filters but encountering difficulties.

    If you are using Rank Math SEO plugin, it might be overriding your custom canonical settings. To address this, you can try using Rank Math’s built-in functionality to set or clear the canonical URL for your dynamic pages.

    Here are the steps you can follow to update the canonical URL in Rank Math:

    1. Navigate to the affected page in your WordPress dashboard.
    2. Look for the Rank Math meta box while editing the page.
    3. Under the Advanced tab, locate the Canonical URL field.
    4. Enter the URL that you want to be the canonical URL for that specific page.
    5. Save or update the page.

    By following these steps within Rank Math, you should be able to set a custom canonical URL for your dynamic pages effectively.

    If you continue to experience issues, please provide more details or screenshots of the problem so we can assist you further.

    Thank you for reaching out with your concern. Let me know if you need any more help.

    Sources:
    How to Fix “Duplicate, Google Chose Different Canonical than User” » Rank Math
    Bulk Editing Options in the Rank Math SEO Plugin
    Alternate Page with Proper Canonical Tag Status in Google Search Console » Rank Math
    How to Fix ‘Duplicate, Submitted URL Not Selected as Canonical’ Issue in Google Search Console » Rank Math

    Anonymous
    Rank Math free

    The pages are loading dynamic content depending on URL parameters. I can’t point to a single canonical URL. That’s why I need to remove the tag.

    Anonymous
    Rank Math free

    I tried placing the filter in functions.php child template file, as mentioned here: https://support.rankmath.com/ticket/cannot-change-canonical-url-on-dynamic-urls/
    and still nothing.

    Hello,

    Could you please share example URLs so we can suggest the filter?

    Meanwhile, you can set them to noindex as Rank Math automatically removes the canonical tag to URLs that are set to noindex.

    Here’s a sample filter you can use and customize:

    add_filter('rank_math/frontend/robots', function ($robots) {
    	$url = home_url($_SERVER['REQUEST_URI']);
    	if (strpos($url, '?dynamic-slug') !== false) {
    		$robots['index'] = "noindex";
    		$robots["follow"] = 'nofollow';
    		return $robots;
    	}
    	return $robots;
    });

    Looking forward to helping you.

    Anonymous
    Rank Math free

    Hello,

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

    Thank you.

    Anonymous
    Rank Math free

    Thank you. I shared the URL via sensitive data. We want the pages to be indexed. The thing is the canonical tag is pointing to the static url of the page. We use those pages to load content depending on GET parameters.

    Here is the filter:
    add_filter( ‘rank_math/frontend/canonical’, function( $canonical ) {
    // Check if the current URL contains the parameter
    if ( isset( $_GET[‘pais’] ) ) {
    // Create a new canonical URL based on the current URL
    $canonical = home_url( $_SERVER[‘REQUEST_URI’] );
    }

    return $canonical;
    });

    Hello,

    If the content of those pages is loaded dynamically it might be running before the functions for the canonical URL which would cause issues even with the filter because it cannot override the canonical at that time.

    In the filter you shared previously, please clear it completely and dump the value of the variable $canonical to the page to make sure the filter is running correctly on the page.

    If it is, please try changing the priority of the filter to something higher like 999 to see if that helps override the current canonical set on the page.

    Don’t hesitate to get in touch if you have any other questions.

    Anonymous
    Rank Math free

    Hello.
    Thanks a lot for your assistance.
    The issue was that as the code is placed in functions.php, the GET variable I was expecting is not set yet. I change the code and used $_SERVER[‘REQUEST_URI’] value to detect if the URL is one of the dynamic pages and then I return false in that case:

    add_filter( ‘rank_math/frontend/canonical’, function( $canonical ) {

    if(strpos($_SERVER[‘REQUEST_URI’],’/dynamic-folder-url’)===0){
    return false;
    }

    return $canonical;
    },1000);

    Hello,

    We are glad that you managed to resolve this issue.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

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

The ticket ‘Setting or clearing canonical tag’ is closed to new replies.