Skip to content

Instantly share code, notes, and snippets.

@fseonline
Created April 26, 2019 10:35
Show Gist options
  • Save fseonline/3180183bcef8af0167edc0cd3c06687d to your computer and use it in GitHub Desktop.
Save fseonline/3180183bcef8af0167edc0cd3c06687d to your computer and use it in GitHub Desktop.
Apply Canonical tag to basic PHP sites
<?php
/**
* Apply correct canonical tag for Forest Drive and Resin
* @author Freelance SEO Essex
* @link https://www.freelanceseoessex.co.uk/
*/
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$dirname = pathinfo($actual_link)['dirname'];
if( $dirname === 'http:' ) {
$dirname = 'http://';
}
if( $dirname === 'https:' ) {
$dirname = 'https://';
}
$basename = pathinfo($actual_link)['basename'];
$extension = pathinfo($actual_link)['extension'];
$canonical_url = $dirname . $basename;
if( $extension === 'php' ) {
$basename = str_replace('.php', '', $basename);
$canonical_url = $dirname . '/' . $basename;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment