Created
April 25, 2017 00:40
-
-
Save wpsmith/9401d2eb179ad8bdb2b2849603eed922 to your computer and use it in GitHub Desktop.
Canonical URL: Always add a trailing slash to the canonical URL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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