How to add dynamic rel="canonical" on HTML head
-
Hi,
I few days ago I’ve updated my site to HTTPS. I’ve read all google’s suggestion for HTTPS migration and even watched their Google IO 2014 HTTPS presentation. On that presentation, Google mentioned to put arel="canonical"
in the HTML head to your site which will point to the HTTPS version of the site. Like –<link href="https://www.example.com" rel="canonical" />
. For achieving that I wrote a simple function on my site like this –function rel_canonical_header() { echo '<link rel="canonical" href="https://www.example.com" />'; } add_action('wp_header', 'rel_canonical_header');
After few days, I’ve realized that this is not a good way to do this, because no matter which page the user be, it will always put the same link i.e
https://www.example.com
in the head. Instead which I want to acheive is to show the exact page or post link where ever the user will be as rel canonical link. So, if a user is visiting the home page, in header it will put<link href="https://www.example.com" rel="canonical" />
, but if a user is visiting a page says Test page then it will put this –<link href="https://www.example.com/test-page" rel="canonical" />
in the header and if the user visiting a post called Test Post, then it will be showed as<link href="https://www.example.com/test-post" rel="canonical" />
.I think this makes more sense of what Google wants the web masters to do. So, please help me and guide me to achieve that.
P.S.: The google presentation link: https://www.youtube.com/watch?v=cBhZ6S0PFCY
- The topic ‘How to add dynamic rel="canonical" on HTML head’ is closed to new replies.