diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 7d8c5ace6a..339e09986d 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -485,7 +485,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { if ( ! empty( $redirect['query'] ) ) { // Remove trailing spaces and end punctuation from certain terminating query string args. - $redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); + $redirect['query'] = preg_replace( "#((^|&)(p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); // Clean up empty query strings. $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' ); diff --git a/tests/phpunit/tests/canonical/noRewrite.php b/tests/phpunit/tests/canonical/noRewrite.php index 48f178a3b8..001e3e3122 100644 --- a/tests/phpunit/tests/canonical/noRewrite.php +++ b/tests/phpunit/tests/canonical/noRewrite.php @@ -34,9 +34,9 @@ class Tests_Canonical_NoRewrite extends WP_Canonical_UnitTestCase { * [0]: Test URL. * [1]: Expected results: Any of the following can be used. * array( 'url': expected redirection location, 'qv': expected query vars to be set via the rewrite AND $_GET ); - * array( expected query vars to be set, same as 'qv' above ) - * (string) expected redirect location - * [3]: (optional) The ticket the test refers to, Can be skipped if unknown. + * array( expected query vars to be set, same as 'qv' above ); + * (string) expected redirect location. + * [3]: (optional) The ticket the test refers to. Can be skipped if unknown. */ return array( array( '/?p=123', '/?p=123' ), @@ -267,11 +267,13 @@ class Tests_Canonical_NoRewrite extends WP_Canonical_UnitTestCase { array( '/page/2/%E2%80%9C', '/page/2/', 20383 ), // Encoded opening curly quote. array( '/page/2/%E2%80%9D', '/page/2/', 20383 ), // Encoded closing curly quote. - array( '/?page_id=1', '/?p=1' ), // Redirect page_id to p (should cover page_id|p|attachment_id to one another. + array( '/?page_id=1', '/?p=1' ), // Redirect page_id to p (should cover page_id|p|attachment_id to one another). array( '/?page_id=1&post_type=revision', '/?p=1' ), array( '/?feed=rss2&p=1', '/?feed=rss2&p=1', 21841 ), array( '/?feed=rss&p=1', '/?feed=rss2&p=1', 24623 ), + + array( '/?comp=East+(North)', '/?comp=East+(North)', 49347 ), ); } }