A canonical tag is an HTML element. If there will be same or similar content available under different URL, canonical tag use to specify which version is the main.
A canonical tag can be placed in the head section of a webpages HTML code, to tells search engines robots that the content of the specified page should be given to the page specified in the tag, rather than to the page on which the tag is placed. This can help search robots in resolving issues with multiple pages on a website that have similar or duplicate content and helps to understand which page should be indexed and returned in search results.
Canonical tags are an important code for website because it help to know duplicate content issues, which can negatively impact a site's search engine rankings. A few key ways that canonical tags can help improve a site's SEO.
The basic code of a canonical tag is as follows.
<link rel="canonical" href="http://www.example.com/page-name.html" />
In the above example, http://www.example.com/page-name.html is the URL of the webpage, tell search engines robots to page index.
The href attribute of the tag should be written in the full URL of the webpage, including the http or https protocol. The specified URL should be accessible and returns a 200 HTTP status code.
To specify the canonical URL for a webpage of same website, the relative URL of the page use as given below
<link rel="canonical" href="/page-name.html" />
The canonical URL can also use for a page on another website, if the website owner allow and the URL is accessible.
<link rel="canonical" href="http://www.example2.com/preferred-page.html" />
It's generally a good practice to include a self-referencing canonical tag on all pages of website to avoid any confusion and specify the preferred version of the page
To specify the preferred version of a mobile page in a canonical tag, use the rel="canonical" link element in the head of the HTML code for the mobile page, and specify the URL of the corresponding desktop version in the href attribute.
An example given below of how the canonical tag would look for a mobile page.
<link rel="canonical" href="http://www.example.com/desktop-page.html" />
In this example, the mobile page is shows that the desktop version of the page at desktop-page.html is the preferred version to be indexed by search engines.
Alternatively, use the link element on the desktop page, and specify the mobile page URL with the rel="alternate" and media="only screen and (max-width: 640px)".
<link rel="alternate" media="only screen and (max-width: 640px)"
href="http://www.example.com/mobile-page.html">
In this way, tag telling to search engines that the mobile version of the page at "http://www.example.com/mobile-page.html" is an alternative for the desktop version for users who are on mobile devices.
When using the rel="alternate" method, it is recommended to also use the rel="canonical" method on the mobile version, to specify that the desktop version is the preferred one.
The URLs which specified in the href attribute are accessible and return a 200 HTTP status code.
Conclusion: canonical tags help search engines understand the structure of the website and the preferred version of a page, which can improve your site's search engine rankings, simplify tracking and analysis, and prevent duplicate content issues.