class Rewrite_Inner_Page_Links{
var $separator;
var $post_rule;
var $comment_rule;
function __construct(){
$this->separator = '/page-';
$this->post_rule = 'archives/([^/]+)('.$this->separator.'([0-9]+))?.html/?$';
$this->comment_rule = 'archives/([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$';
if( !is_admin() || defined( 'DOING_AJAX' ) ) :
add_filter( 'wp_link_pages_link', array( $this, 'inner_page_link_format' ), 10, 2 ); // for inner pages
add_filter( 'get_comments_pagenum_link', array( $this, 'comment_page_link_format' ) );
add_filter( 'redirect_canonical', array( $this, 'cancel_redirect_for_paged_posts' ), 10, 2 );
endif;
if( is_admin() ) :
add_filter( 'rewrite_rules_array', array( $this, 'pagelink_rewrite_rules' ) );
endif;
}
/**
* 修改post分页链接的格式
* @param string $link
* @param int $number
* @return string
*/
function inner_page_link_format( $link, $number ){
if( $number > 1 ){
$link = preg_replace( "%(\.html)/(\d*)%", $this->separator."$2$1", $link );
function comment_page_link_format( $result ){
// From hello-world.html/comment-page-1#comments to hello-world/comment-page-1.html#comments
if( strpos( $result, '.html/' ) !== false ){
* 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录
function pagelink_rewrite_rules( $rules ){
foreach ($rules as $rule => $rewrite) {
if ( $rule == '([^/]+).html(/[0-9]+)?/?$' || $rule == '([^/]+).html/comment-page-([0-9]{1,})/?$' ) {
$new_rule[ $this->post_rule ] = 'index.php?name=$matches[1]&page=$matches[3]';
$new_rule[ $this->comment_rule ] = 'index.php?name=$matches[1]&cpage=$matches[2]';
* @param string $requested_url
function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){
if( is_single() && $wp_query->get( 'page' ) > 1 ){
new Rewrite_Inner_Page_Links();
class Rewrite_Inner_Page_Links{
$this->post_rule = 'archives/([^/]+)('.$this->separator.'([0-9]+))?.html/?$';
$this->comment_rule = 'archives/([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$';
if( !is_admin() || defined( 'DOING_AJAX' ) ) :
add_filter( 'get_comments_pagenum_link', array( $this, 'comment_page_link_format' ) );
add_filter( 'redirect_canonical', array( $this, 'cancel_redirect_for_paged_posts' ), 10, 2 );
add_filter( 'rewrite_rules_array', array( $this, 'pagelink_rewrite_rules' ) );
function inner_page_link_format( $link, $number ){