Skip to content

Commit 7db2007

Browse files
committed
Make Canonical feeds and paging play nice with each other and with PATHINFO URL structures. fixes WordPress#8076
git-svn-id: https://develop.svn.wordpress.org/trunk@9697 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d7351f9 commit 7db2007

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

wp-includes/canonical.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,40 +133,49 @@ function redirect_canonical($requested_url=null, $do_redirect=true) {
133133
if ( !$redirect_url )
134134
$redirect_url = $requested_url;
135135
$paged_redirect = @parse_url($redirect_url);
136-
while ( preg_match( '#page/[0-9]+?(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/feed(/[a-z0-9-]*?(/+)?)?$#', $paged_redirect['path'] ) || preg_match( '#comment-page-[0-9]+/?$#', $paged_redirect['path'] ) ) {
136+
while ( preg_match( '#/page/[0-9]+?(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/(feed|rss|rdf|atom|rss2)(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $paged_redirect['path'] ) ) {
137137
// Strip off paging and feed
138138
$paged_redirect['path'] = preg_replace('#/page/[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging
139-
$paged_redirect['path'] = preg_replace('#/feed(/[a-z0-9-]*?(/+)?)?$#', '/', $paged_redirect['path']); // strip off any existing feed
140-
$paged_redirect['path'] = preg_replace('#comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging
139+
$paged_redirect['path'] = preg_replace('#/(feed|rss2?|rdf|atom)(/+)?$#', '/', $paged_redirect['path']); // strip off feed endings
140+
$paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging
141+
}
142+
143+
$addl_path = '';
144+
if ( is_feed() ) {
145+
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'feed/' . ( ( 'rss2' == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
146+
$redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
141147
}
142148

143-
$paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); // strip off trailing /index.php/
144149
if ( get_query_var('paged') > 0 ) {
145150
$paged = get_query_var('paged');
146-
if ( $paged > 1 && !is_single() ) {
147-
$paged_redirect['path'] = trailingslashit($paged_redirect['path']);
148-
if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
149-
$paged_redirect['path'] .= 'index.php/';
150-
$paged_redirect['path'] .= user_trailingslashit("page/$paged", 'paged');
151-
} elseif ( !is_single() ) {
152-
$paged_redirect['path'] = user_trailingslashit($paged_redirect['path'], 'paged');
151+
$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
152+
if ( !is_feed() ) {
153+
if ( $paged > 1 && !is_single() ) {
154+
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged');
155+
} elseif ( !is_single() ) {
156+
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit($paged_redirect['path'], 'paged');
157+
}
158+
} elseif ( $paged > 1 ) {
159+
$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
153160
}
154161
}
155-
if ( is_feed() ) {
156-
$paged_redirect['path'] = user_trailingslashit( trailingslashit( $paged_redirect['path'] ) . 'feed/' . ( ( 'rss2' == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
157-
}
162+
158163
if ( get_option('page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) {
159-
$paged_redirect['path'] = user_trailingslashit( trailingslashit( $paged_redirect['path'] ) . 'comment-page-' . get_query_var('cpage'), 'commentpaged' );
164+
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' );
165+
$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
160166
}
167+
168+
$paged_redirect['path'] = trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/
169+
if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
170+
$paged_redirect['path'] .= 'index.php/';
171+
$paged_redirect['path'] .= $addl_path;
161172
$redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
162173
$redirect['path'] = $paged_redirect['path'];
163-
$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
164-
$redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
165-
$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
166174
}
167175
}
168176

169177
// tack on any additional query vars
178+
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
170179
if ( $redirect_url && !empty($redirect['query']) ) {
171180
if ( strpos($redirect_url, '?') !== false )
172181
$redirect_url .= '&';
@@ -205,7 +214,7 @@ function redirect_canonical($requested_url=null, $do_redirect=true) {
205214
$redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
206215

207216
// Remove redundant leading ampersands
208-
$redirect['query'] = preg_replace( '#^\??&+#', '', $redirect['query'] );
217+
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
209218
}
210219

211220
// strip /index.php/ when we're not using PATHINFO permalinks

0 commit comments

Comments
 (0)