diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php
index 4c981f9..7c79e76 100644
--- a/src/wp-includes/canonical.php
+++ b/src/wp-includes/canonical.php
@@ -10,6 +10,51 @@
  */
 
 /**
+ * Display the proper URL in the browser.
+ * 
+ * In cituations a history.replaceState is possible, the URL will be
+ * rewritten using JavaScript. Otherwise a 301 redirect will be used
+ * to send the use to the correct URL.
+ *
+ * @param string $requested_url Optional. The URL that was requested, used to
+ *		figure if redirect is needed.
+ * @param bool $do_redirect Optional. Redirect to the new URL.
+ *
+ * @return string|void The string of the URL, if redirect needed.
+ */
+function maybe_redirect_canonical( $requested_url = null, $maybe_redirect = true ) {
+
+	$redirect_url = redirect_canonical( $requested_url, false );
+
+	/**
+	 * Filter whether JavaScript's history API can be used for canonical 
+	 * redirects.
+	 *
+	 * @param bool Use the JavaScript history API.
+	 */
+	$use_javascript_history_api = apply_filters( 'wp_javascript_redirect_canonical', true );
+
+	if ( $maybe_redirect && $use_javascript_history_api ) {
+		add_action( 'wp_head', 'redirect_canonical_history_replace', 5 );
+		return;
+	}
+	elseif ( $maybe_redirect ) {
+		// protect against chained redirects
+		if ( !redirect_canonical($redirect_url, false) ) {
+			wp_redirect($redirect_url, 301);
+			exit();
+		} else {
+			// Debug
+			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
+			return;
+		}
+	} else {
+		return $redirect_url;
+	}
+}
+
+
+/**
  * Redirects incoming links to the proper URL based on the site url.
  *
  * Search engines consider www.somedomain.com and somedomain.com to be two
@@ -96,6 +141,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 	}
 
 	if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
+		add_filter( 'wp_javascript_redirect_canonical', '__return_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 +163,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 
 	// These tests give us a WP-generated permalink
 	if ( is_404() ) {
+		add_filter( 'wp_javascript_redirect_canonical', '__return_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 +418,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 ) {
+		add_filter( 'wp_javascript_redirect_canonical', '__return_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 ) {
+			add_filter( 'wp_javascript_redirect_canonical', '__return_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 ) {
+			add_filter( 'wp_javascript_redirect_canonical', '__return_false' );
+		}
 
 		// Remove redundant leading ampersands
 		$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
@@ -411,8 +470,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 ) {
+		add_filter( 'wp_javascript_redirect_canonical', '__return_false' );
 		$redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
+	}
 
 	// Always trailing slash the Front Page URL
 	if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
@@ -425,17 +486,23 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 		$redirect['host'] = $original['host'];
 
 	$compare_original = array( $original['host'], $original['path'] );
+	$original_origin = array( $original['scheme'], $original['host'] );
 
-	if ( !empty( $original['port'] ) )
+	if ( !empty( $original['port'] ) ) {
 		$compare_original[] = $original['port'];
+		$original_origin[] = $original['port'];
+	}
 
 	if ( !empty( $original['query'] ) )
 		$compare_original[] = $original['query'];
 
 	$compare_redirect = array( $redirect['host'], $redirect['path'] );
+	$redirect_origin = array( $redirect['scheme'], $redirect['host'] );
 
-	if ( !empty( $redirect['port'] ) )
+	if ( !empty( $redirect['port'] ) ) {
 		$compare_redirect[] = $redirect['port'];
+		$redirect_origin[] = $redirect['port'];
+	}
 
 	if ( !empty( $redirect['query'] ) )
 		$compare_redirect[] = $redirect['query'];
@@ -449,6 +516,10 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 			$redirect_url .= '?' . $redirect['query'];
 	}
 
+	if ( $redirect_origin != $original_origin ) {
+		add_filter( 'wp_javascript_redirect_canonical', '__return_false' );
+	}
+
 	if ( ! $redirect_url || $redirect_url == $requested_url ) {
 		return;
 	}
@@ -463,6 +534,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,6 +548,10 @@ 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 ) {
+		add_filter( 'wp_javascript_redirect_canonical', '__return_false' );
+	}
+
 	// yes, again -- in case the filter aborted the request
 	if ( ! $redirect_url || $redirect_url == $requested_url ) {
 		return;
@@ -595,3 +672,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 esc_js( $canonical_url ); ?>' ));
+	</script>
+	<?php
+}
\ No newline at end of file
diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
index f9fd9c4..f2bdfee 100644
--- a/src/wp-includes/default-filters.php
+++ b/src/wp-includes/default-filters.php
@@ -389,7 +389,7 @@ add_filter( 'style_loader_src', 'wp_style_loader_src', 10, 2 );
 add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
 
 // Canonical
-add_action( 'template_redirect', 'redirect_canonical' );
+add_action( 'template_redirect', 'maybe_redirect_canonical' );
 add_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
 
 // Shortcodes
