Home / Admin / Set current URL as the Canonical URL and ignore query strings
Duplicate Snippet

Embed Snippet on Your Site

Set current URL as the Canonical URL and ignore query strings

This snippet searches for a specific string in the current URL and if found, it set the current URL as the Canonical URL and ignores any query strings.

Important: Replace '/home-search/listing/' with the string to be searched in the current URL.

300+
Code Preview
php
<?php
add_filter( 'aioseo_canonical_url', 'aioseo_filter_canonical_url' );
function aioseo_filter_canonical_url( $url ) {
	if (strpos($_SERVER['REQUEST_URI'],'/home-search/listing/') !== false) {
		$url = home_url( $_SERVER['REQUEST_URI'] );
		if (strpos($url,'?') !== false){
				$url = substr( $url, 0, strpos($url,'?') );
		}
	}
	return $url;
}

Comments

Add a Comment