diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php
index 4caaa65..1f385ba 100644
--- a/src/wp-includes/canonical.php
+++ b/src/wp-includes/canonical.php
@@ -59,6 +59,10 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 		return;
 	}
 
+	// Redirect to the canonical URL using JavaScript's history.replaceState()
+	// function. If this is false, a 301 redirect will be used.
+	$use_javascript_history_api = true;
+
 	if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
 		// build the URL in the address bar
 		$requested_url  = is_ssl() ? 'https://' : 'http://';
@@ -96,6 +100,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 	}
 
 	if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
+		$use_javascript_history_api = false;
 		if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
 			$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url );
 			$redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
@@ -117,6 +122,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 
 	// These tests give us a WP-generated permalink
 	if ( is_404() ) {
+		$use_javascript_history_api = false;
 
 		// Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
 		$id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') );
@@ -371,17 +377,29 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 	$redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);
 
 	// Remove trailing spaces from the path
-	$redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
+	$count = 0;
+	$redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'], -1, $count );
+	if ( $count > 0 ) {
+		$use_javascript_history_api = false;
+	}
 
 	if ( !empty( $redirect['query'] ) ) {
 		// Remove trailing spaces from certain terminating query string args
-		$redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
+		$count = 0;
+		$redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'], -1,  $count );
+		if ( $count > 0 ) {
+			$use_javascript_history_api = false;
+		}
 
 		// Clean up empty query strings
 		$redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
 
 		// Redirect obsolete feeds
-		$redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] );
+		$count = 0;
+		$redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'], -1, $count );
+		if ( $count > 0 ) {
+			$use_javascript_history_api = false;
+		}
 
 		// Remove redundant leading ampersands
 		$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
@@ -411,8 +429,10 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 	}
 
 	// Strip multiple slashes out of the URL
-	if ( strpos($redirect['path'], '//') > -1 )
+	if ( strpos($redirect['path'], '//') > -1 ) {
+		$use_javascript_history_api = false;
 		$redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
+	}
 
 	// Always trailing slash the Front Page URL
 	if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
@@ -429,6 +449,8 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 	if ( !empty( $original['port'] ) )
 		$compare_original[] = $original['port'];
 
+	$compare_original_origin = $compare_original;
+
 	if ( !empty( $original['query'] ) )
 		$compare_original[] = $original['query'];
 
@@ -437,6 +459,8 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 	if ( !empty( $redirect['port'] ) )
 		$compare_redirect[] = $redirect['port'];
 
+	$compare_redirect_origin = $compare_redirect;
+
 	if ( !empty( $redirect['query'] ) )
 		$compare_redirect[] = $redirect['query'];
 
@@ -449,6 +473,10 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 			$redirect_url .= '?' . $redirect['query'];
 	}
 
+	if ( $compare_redirect_origin != $compare_original_origin ) {
+		$use_javascript_history_api = false;
+	}
+
 	if ( ! $redirect_url || $redirect_url == $requested_url ) {
 		return;
 	}
@@ -463,6 +491,8 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 		$requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
 	}
 
+	$pre_filter_redirect_url = $redirect_url;
+
 	/**
 	 * Filter the canonical redirect URL.
 	 *
@@ -475,12 +505,19 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 	 */
 	$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
 
+	if ( $pre_filter_redirect_url !== $redirect_url ) {
+		$use_javascript_history_api = false;
+	}
+
 	// yes, again -- in case the filter aborted the request
 	if ( ! $redirect_url || $redirect_url == $requested_url ) {
 		return;
 	}
 
-	if ( $do_redirect ) {
+	if ( $do_redirect && $use_javascript_history_api ) {
+		add_action( 'wp_head', 'redirect_canonical_history_replace', 5 );
+	}
+	elseif ( $do_redirect ) {
 		// protect against chained redirects
 		if ( !redirect_canonical($redirect_url, false) ) {
 			wp_redirect($redirect_url, 301);
@@ -595,3 +632,20 @@ function wp_redirect_admin_locations() {
 		exit;
 	}
 }
+
+/**
+ * Replace the URL as displayed in the browser with the canonical URL
+ */
+function redirect_canonical_history_replace() {
+	$canonical_url = redirect_canonical( null, false );
+	?>
+	<script>
+	(function( window, url ){
+		var history = window.history;
+		if ( history.replaceState ) {
+			history.replaceState( {}, '', url+window.location.hash );
+		}
+	}( window, '<?php echo $canonical_url; ?>' ));
+	</script>
+	<?php
+}
\ No newline at end of file
