Setting up rel canonical with drupal

Hi
i wanted to add a rel canonical tag to all pages on my site
inside page.tpl.php , i added:

$link1= request_uri();
$link2= 'https://www.mysite.com' . $link1;
print '<link rel="canonical" href="' .$link2 .'"/>';

now the problem is that whatever URL is typed the canonical link changes

for example https://www.mysite.com/page1 has a different canonical link than https://www.mysite.com/page1? and https://www.mysite.com/page1?q=

is there a way to hard code canonical links so that they get the real URL path of my drupal page?

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/22/feed