在对url-rewriting add-rewrite-rule 和rewrite-rules
我被介绍给Redirect Canonical
根据站点URL将传入链接重定向到正确的URL。
搜索引擎考虑www.somedomain。com和somedomain。com是两个不同的URL,当它们都指向同一位置时。此SEO增强通过将所有传入链接重定向到其中一个来防止对重复内容的惩罚。
正因为如此,所有分页的页面都被重定向到了域。com/lorem=ipsum/photos当我想要域名时。com/lorem ipsum/photos/page/2
因此,我需要改变redirect_canonical
使用过滤器redirect_canonical
PS: 如果你写了一个好的重写规则,但它会把你带到另一个页面,那么它可以重定向\\u规范地按照你的重写规则操作
我希望这对其他人有帮助,抱歉英语不好。
你可以检查我的最终功能。php
add_action( \'after_setup_theme\', \'rm_setup_theme\' );
function rm_setup_theme() {
add_filter( \'rewrite_rules_array\', \'rm_custom_rules\' );
add_filter( \'query_vars\', \'rm_custom_vars\' );
add_filter( \'template_redirect\', \'rm_custom_redirect\' );
add_filter( \'redirect_canonical\', \'rm_redirect_canonical\', 10, 2 );
}
function rm_custom_rules( $rules ) {
$r = array(
\'([^/]+)(/[0-9]+)?/photos/page/?([0-9]{1,})/?$\' => \'index.php?name=$matches[1]&page=$matches[2]&photos=1&paged=$matches[3]\',
\'([^/]+)(/[0-9]+)?/photos/?$\' => \'index.php?name=$matches[1]&page=$matches[2]&photos=1\',
);
return array_merge( $r, $rules );
}
function rm_custom_vars( $qv ) {
array_push( $qv, \'photos\' );
return $qv;
}
function rm_custom_redirect( ) {
global $wp_query;
$f = $wp_query->get( \'photos\' );
if ( $f ) {
require TEMPLATEPATH . \'/content-photos.php\';
exit;
}
}
function rm_redirect_canonical( $redirect_url, $requested_url ) {
global $wp_rewrite;
// Abort if not using pretty permalinks, is a feed or is not a singular post page
if ( !$wp_rewrite->using_permalinks() || is_feed() || !is_singular( \'post\' ) )
return $redirect_url;
// Get the original query parts
$redirect = @parse_url( $requested_url );
$original = $redirect_url;
if ( !isset( $redirect[\'query\'] ) )
$redirect[\'query\'] = \'\';
// If paged append pagination
if ( get_query_var( \'paged\' ) > 0 ) {
$paged = (int) get_query_var( \'paged\' );
$redirect[\'query\'] = remove_query_arg( \'paged\', $redirect[\'query\'] );
if ( $paged > 1 )
$redirect_url .= user_trailingslashit( "page/$paged", \'paged\' );
}
if ( $redirect_url == $original )
return $original;
// tack on additional query vars
$redirect[\'query\'] = preg_replace( \'#^\\??&*?#\', \'\', $redirect[\'query\'] );
if ( $redirect_url && !empty($redirect[\'query\']) ) {
parse_str( $redirect[\'query\'], $_parsed_query );
$_parsed_query = array_map( \'rawurlencode\', $_parsed_query );
$redirect_url = add_query_arg( $_parsed_query, $redirect_url );
}
return $redirect_url;
}
define( \'SITE_NAME\', get_bloginfo( \'name\' ) );
define( \'SITE_URL\', home_url() );
define( \'THEME_URL\', get_template_directory_uri() . \'/\' );