How to change the permalink structure of a master page?

To remove pagination prefix page in home page you should:

  • add a rewrite rule which will translate the new link format,
  • prevent redirection to address containing page,
  • replace URLs in paging links.

By creating pagination links, WordPress appends query var paged with mentioned prefix to url. Therefore, without modification, the links would look like example.com/3/page/3.

Next thing is redirection. WP redirects incoming links to the proper URL based on the site url.Thanks to our rewrite rule, for the example.com/3 url, the results of the third page will be displayed (paged=3). But WP check that correct url for paged=3 should be example.com/3/page/3 which is different from example.com/3.
Therefore, a redirection will be made.

add_action( 'init', 'se323035_structure', 25 );
add_filter( 'paginate_links',       'se323035_paginate_links' );
add_filter( 'redirect_canonical',   'se323035_redirect_canonical', 20, 2 );

function se323035_structure()
{
    global $wp_rewrite;
    if ( $wp_rewrite->using_permalinks() )
        add_rewrite_rule( '([0-9]+)/?$', 'index.php?&paged=$matches[1]', 'top');
}

function se323035_paginate_links( $link )
{
    global $wp_rewrite;
    if ( is_home() && $wp_rewrite->using_permalinks() ) {
        $original = @parse_url( $link );
        $home = @parse_url( home_url("https://wordpress.stackexchange.com/") );
        if ( ! isset( $original['path'] ) )
            $original['path'] = '';

        $original_path = $original['path'];
        $original_path_end = $original['path'];
        if ( isset($home['path']) )
        {
            $home['path'] = trim($home['path']);
            $home['path'] = untrailingslashit($home['path']);
            if ( !empty($home['path']) && $home['path'] != "https://wordpress.stackexchange.com/" )
                $original_path_end = str_replace( $home['path'], '', $original['path']);
        }
        //
        // if original url match to our link structure do not redirect to '/page/{number}'
        if ( preg_match( '|^/?[0-9]+/?$|', $original_path_end) ) {
            $link = home_url( "https://wordpress.stackexchange.com/" );
        }
        elseif ( preg_match( "|/(\d+/)?$wp_rewrite->pagination_base/\d+/?$|", $original_path_end) )
            $link = preg_replace( "|/(\d+/)?$wp_rewrite->pagination_base/(\d+/?)$|", '/${2}', $link );
    }

    return $link;
}

function se323035_redirect_canonical( $redirect_url, $requested_url )
{
    global $wp_rewrite;

    if ( is_home() && !empty($redirect_url) )
    {
        $original = @parse_url( $requested_url );
        if ( ! isset( $original['path'] ) )
            $original['path'] = '';

        $original_path = $original['path'];
        $home = @parse_url( home_url("https://wordpress.stackexchange.com/") );
        if ( isset($home['path']) )
        {
            $home['path'] = trim($home['path']);
            $home['path'] = untrailingslashit($home['path']);
            if ( !empty($home['path']) && $home['path'] != "https://wordpress.stackexchange.com/" )
                $original_path = str_replace( $home['path'], '', $original['path']);
        }
        // if original url match to our link structure don't redirect to '/page/{number}'
        if ( preg_match( '|^/?[0-9]+/?$|', $original_path) )
            return '';
    }
    return $redirect_url;
}

The new link format can not be used if the permalink structure is set to /%post_id%.

Leave a Comment