I'm trying to make some SEO in my website, I'm using the Google Webmaster Tools, but I'm getting this error all the time:

The Specified Canonical Link Points to a Different page

I already added the following line of code in the header of a global file:

<link rel="canonical" href="<? php echo DEFAULT_CANONICAL; ?>" >

Also I added a 301 redirection to my current domain in my .htaccess, I've tried everything but the error keeps appearing.

The value of DEFAULT_CANONICAL is https://domain_of_my_website/.$_SERVER['REQUEST_URI'])

What can I do in this case?

  • 1
    What is the URL of the page you are on? What is the URL in the canonical tag? <? php echo - Do you really have a space between <? and php? That is not valid and will be sent as plain text to the browser (although that would be obvious by looking at the HTML source). (?) – MrWhite Mar 6 '20 at 18:16
  • I have it on a global file, so the logic is that every time it takes to a user to a determined URL, it's going to have that header – danhergir Mar 6 '20 at 18:34
  • 1
    Yes, but what are the actual URLs being reported - do they differ? Is there a space in <? php in your actual code, or is it a typo in your question? (That would certainly cause the problem you are getting.) However, you can't reliably use $_SERVER['REQUEST_URI'] to refer to the canonical URL. Unless you've manually overridden this? Since PHP's REQUEST_URI variable contains the query string, which could be manipulated by a user. In other words, it's vulnerable to XSS. – MrWhite Mar 6 '20 at 19:51
<link rel="canonical" href="<? php echo DEFAULT_CANONICAL; ?>" >

There is an error in the opening PHP tag that will result in this error, and without any feedback to comments we can only assume this is as written in your actual code - not simply a typo in your question?

The erroneous space in <? php (it should be <?php) will fail to get the enclosed PHP interpreted as PHP code and will be sent to the browser as written - as malformed HTML! If this is the case then "The Specified Canonical Link" will indeed "Point to a Different page" - it won't point to any valid page!

You need to remove the erroneous space in the opening PHP tag, or use short-echo tags instead (for this kind of templating):

<link rel="canonical" href="<?=DEFAULT_CANONICAL?>" >

The value of DEFAULT_CANONICAL is https://domain_of_my_website/.$_SERVER['REQUEST_URI'])

However, as noted in comments, you can't reliably construct the canonical URL by simply using PHP's $_SERVER['REQUEST_URI'] variable without additional parsing. This variable contains query string parameters that can be manipulated by the user - changing the canonical URL in the HTML and potentially making your site vulnerable to XSS attacks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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