Cannot change canonical url on dynamic urls

#716183
  • Resolved Tim Klatt
    Rank Math business

    I have a main page called /stelle and I use a template in my theme for it. Depending on a query (?jobId), the content of the page is displayed differently. Every page is different and the actual page (/site) points to a soft 404 because no content is loaded. I have now tried to adapt the canonical tag on every dynamic page using my template using this code:

    add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
    $url = home_url() . '/stelle?jobId=' . $job->id;
    return $url;
    });

    its not important where the $job->id comes from and it is definitely a filled variable. But the canonical tag on each site, for example [URL]/stelle?jobId=415124 is still [URL]/stelle

    I even tried to maximize the priority but nothing helps. What am I doing wrong here?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello, I’m sorry to hear about the issue you’re facing with changing the canonical URL on your dynamic pages. It seems like you’re trying to dynamically change the canonical URL using a filter in your theme’s functions file, but it’s not reflecting the changes on the pages.

    Based on the information provided, it seems like you’re using a custom filter to modify the canonical URL for dynamic pages. However, it’s possible that the filter isn’t being applied correctly or there might be a conflict with other plugins or theme functionalities.

    To further investigate and resolve this issue, I recommend reaching out to our support team directly from here. Our support team will be able to assist you in identifying the cause of the issue and provide you with a solution to dynamically change the canonical URL for your dynamic pages.

    If you have any other questions or need further assistance, please feel free to let us know.

    Sources:
    Alternate Page with Proper Canonical Tag Status in Google Search Console » Rank Math
    How to Fix ‘Duplicate Without User-Selected Canonical’ Status in Google Search Console » Rank Math
    How to Fix “Duplicate, Google Chose Different Canonical than User” » Rank Math
    How to Fix ‘Duplicate, Submitted URL Not Selected as Canonical’ Issue in Google Search Console » Rank Math
    Rank Math Sitemap Issues and Their Fixes » Rank Math

    Hello,

    Could you please share the actual URL so we can check? You can add it in the sensitive data section.

    Meanwhile, are you trying to achieve a self-referencing canonical on those?

    If so, please try applying this filter on your site to set a self-referencing canonical on URLs:

    add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
    	$url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    	if(strpos($url,'?jobId=') !== false){
    		return $url;
    	}
    	
    	return $canonical;
    });

    And here’s a guide to adding the filter to your website:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Let us know how this goes.

    Tim Klatt
    Rank Math business

    Hello,

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

    Thank you.

    Tim Klatt
    Rank Math business

    I tried the code you gave me but still refers to wrong canonical url

    Hello,

    Please replace the existing code with this one and check if it resolves the issue.

    add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
        // Check if the current URL contains the 'jobId' parameter
        if ( isset( $_GET['jobId'] ) ) {
            // Create a new canonical URL based on the current URL
            $canonical = home_url( $_SERVER['REQUEST_URI'] );
        }
    
        return $canonical;
    });

    This code checks if the ‘jobId’ parameter is present in the URL. If it is, it dynamically sets the canonical URL based on the current URL.

    If the problem persists, we might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.

    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Tim Klatt
    Rank Math business

    Hey, thanks for helping. I was confused because I already tried the exact same code you gave me, but now I tried something different: Actually I had inserted the filter into the template file but that didn’t work. Now I tried to insert the code into functions.php for the first time and lo and behold, it works. Is it intended that the filters do not go into template files from themes?

    Hello,

    We are glad that worked.

    Regarding the filter code, it should be applied to your theme’s functions.php file. You can also use other methods mentioned in this guideline: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps and please don’t hesitate to let us know if you have any other questions.
     
    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Cannot change canonical url on dynamic urls’ is closed to new replies.