PHP Code:
<?php
$right = 'http://www.website.com/page.php';
$wrong = 'http://www.website.com/page.php?id=kjhjkhasdfkjhsfdkjshdf';
$wrong_after = substr($wrong, 0, strpos($wrong, '?')); // This line is the key
echo 'Right ' . $right . '<br /><br />';
echo 'Wrong: ' . $wrong . '<br /><br />';
echo 'Wrong changed: ' . $wrong_after . '<br /><br />';
// $_SERVER[] vars below might also be helpful:
echo '$_SERVER[\'PHP_SELF\']: ' . $_SERVER['PHP_SELF'] . '<br />';
echo '$_SERVER[\'REQUEST_URI\']: ' . $_SERVER['REQUEST_URI'];
?>
The above code will output:
Code:
Right http://www.website.com/page.php
Wrong: http://www.website.com/page.php?id=kjhjkhasdfkjhsfdkjshdf
Wrong changed: http://www.website.com/page.php
$_SERVER['PHP_SELF']: /page.php
$_SERVER['REQUEST_URI']: /page.php?id=kjhjkhasdfkjhsfdkjshdf
So perhaps:
PHP Code:
<link rel="canonical" href="http://www.website.com<?php echo $_SERVER['PHP_SELF'] ?>">
Good luck,
J
Bookmarks