Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created April 25, 2017 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/9401d2eb179ad8bdb2b2849603eed922 to your computer and use it in GitHub Desktop.
Save wpsmith/9401d2eb179ad8bdb2b2849603eed922 to your computer and use it in GitHub Desktop.
Canonical URL: Always add a trailing slash to the canonical URL.
<?php
namespace WPS\canonical;
add_filter( 'get_canonical_url', 'WPS\canonical\get_canonical_url' );
/**
* Add a trailing slash to the end of the path.
*
* @param string $canonical_url Canonical URL.
* @return string Maybe modified URL.
*/
function get_canonical_url( $canonical_url ) {
if ( "/" === substr( $canonical_url, -1 ) ) {
return $canonical_url;
}
$url = parse_url( $canonical_url );
$surl = $url['scheme'] . "://" . $url['host'] . trailingslashit( $url['path'] ) . '?' . $url['query'] . '#' . $url['fragment'];
return $surl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment