PHP Snippet 1:
<?php
$fullurl = ($_SERVER['REQUEST_URI']);
$trimmed = trim($fullurl, ".php");
$canonical = rtrim($trimmed, '/') . '/';
?>
PHP Snippet 2:
<link rel="canonical" href="https://example.com<?php echo $canonical ?>" />
PHP Snippet 3:
<?php
// get the rigth protocol
$protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
// simply render canonical base on the current http host ( multiple host ) + requests
echo $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>
PHP Snippet 4:
https://example.com/test.php
PHP Snippet 5:
https://example.com/test.php/anotherThing.php
PHP Snippet 6:
https://example.com/anotherThing.php
PHP Snippet 7:
$url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . strtok($_SERVER['REQUEST_URI'], '?');
PHP Snippet 8:
$url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
PHP Snippet 9:
echo '<link rel="canonical" href="' . $url . '" />';