Hi,
This 2012 post
[
webmasterworld.com...]
shows a way to elegantly generate a correct rel=canonical element dynamically for each page using the following PHP code:
<link rel="canonical" href="<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];echo $url;?>">
Is there any reason why this approach needs any update given this is 3 years old?
It looks to be working fine to me. But I'm not sure how I'd test it beyond just looking at the code behind the page.
However, as some of you may know, my copy/paste approach is sort of spray-and-pray and while it looks good to me, I have no way of truly knowing it is working the way it should.
Thanks!
-C