Index: canonical.php
===================================================================
--- canonical.php	(revision 14267)
+++ canonical.php	(working copy)
@@ -82,6 +82,15 @@
 	// These tests give us a WP-generated permalink
 	if ( is_404() ) {
 		$redirect_url = redirect_guess_404_permalink();
+		
+		// #13125: redirect p=NNN links to page_id=NNN (if page exists) instead of 404ing
+		if( $id = get_query_var('p') ) {
+			$type = $wpdb->get_var( $wpdb->prepare("SELECT post_type FROM $wpdb->posts WHERE ID = %d", $id ) );
+			if($type && 'page' == $type) {
+				$redirect['query'] = remove_query_arg(array('p', 'page_id'), $redirect['query']);
+				$redirect_url = get_permalink($id);
+			}
+		}
 	} elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
 		// rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
 		if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) {
@@ -387,4 +396,4 @@
 
 add_action('template_redirect', 'redirect_canonical');
 
-?>
+?>
\ No newline at end of file
