PHP script to create rel = canonical tag
The canonical tag specifies the URL to be indexed when multiple URLs end up on the same page or content moved to a new page.
Google has recognized the rel = canonical directive between two different areas to address duplicate content issues since December 15, 2009.
In June 2012, it became an IETF standard called RFC 6596.
It is possible that this tag will solve the problem of a small part of webmasters, which affects most of them even more.
Indeed, search engines do not always understand when a page is copied, what is an original, and what is a copy. What if the coper adds a canonical tag to its copy?
Wordpress users in this game have the advantage of having the tag enabled by default, unlike static sites.
Therefore, you should add a tag to all web pages, and to avoid tedious editing work, a few PHP lines that need to be added to the page template can do this automatically...
Duplicate problem
Duplicate content occurs incorrectly, but not necessarily intentionally, when a web page becomes available to search engine robots at two different URLs.
This is especially true for CMS, which can access pages for different options, as detailed in a Google blog article:
Home page access:
http://www.example.com/mypage.php?node=mykey
Access by category page:
https://www.example.com/mypage.php?node=mykey&category=mycat
Access with session ID:
https://www.example.com/mypage.php?node=mykey&sessionid=1234
Access by number:
https://www.scriptol.com/?1234
Custom URL for link:
https://www.example.com/key1-key2-key3
There are several disadvantages. On the one hand, PageRank from backlinks will be distributed to different URLs, on the other hand, the Google robot will have to choose a unique URL for its pointer, and this is not necessarily the one that the webmaster would like to highlight!
To solve this problem, Google, Yahoo and Live Search offer a new label to insert into the HEAD section of the page.
<link rel = «canonical» href = «https://www.example.com/key1-key2-key3 »/>
It could be a webmaster that will insert it, or a CMS that will automatically generate this label... in the next version.
Source Google Webmaster Central. The same announcement was made on the Yahoo blog and the Microsoft blog .
Canonical lighthouse
It is placed in the <head> section and in the form <link rel = "canonical" href =" url "/>
For example, a link to this page:
<link rel="canonical" href="https://www.iqlevsha.ru/scripts/canonical.php" />
It can be produced automatically in PHP with the following code:
<link rel="canonical" href="https://www.iqlevsha.ru<?php echo $_SERVER['PHP_SELF']; ?>" />
Replacing https://www.iqlevsha.ru with the domain of your site .
Common code
If your server is not configured to recognize the PHP_SELF variable, you can also try this longer code:
<link rel="canonical" href="https://www.example.com <?php echo substr(__FILE__, strlen($_SERVER['DOCUMENT_ROOT'])); ?>" />
Thus, the same code can be used and automatically placed on all your pages.
In fact, you can make a generic domain also with the variable $ _ SERVER ["HTTP _ HOST"], but if you can access the site with or without www, you should avoid it.
Reference: 5 common mistakes with canonical tag. These are mistakes that Google often faces.
Long story short:
- Use tag with paging. Because if several pages of the same article point to the first, this is a mistake.
- Relative links in the URL: since the page may not be placed on the desired site, the resulting effect is the opposite of the desired .
- Multiple tags on the same page. They will be ignored.
- The category page contains a tag that points to the article .
- Tag inside "body." It must be in the <head> section or ignored.