user1383029
user1383029

Reputation: 2125

Wordpress add_rewrite_rule redirects if page is startpage

I do a custom wordpress rewrite with the following code:

function add_rewrite_rules() {
    add_rewrite_rule(
        '^mypath/([A-z0-9\-\_\,]+)/?$', 
        'index.php?page_id=2&tags=$matches[0]', 
        'top'
    );
}
add_action('init', 'add_rewrite_rules');

This works fine: When I open /mypath/tag1,tag2,tag3/ the page with the page_id==2 is shown, and GET parameter tags contains tag1,tag2,tag3. The path stays /mypath/tag1,tag2,tag3/

But there is one exception: When the page with the page_id==2 is marked as the wordpress startpage, then he forgets everything and redirects to /

Wordpress seems to redirect to WP_HOME, if the page is the startpage, but I want it to stay on /mypath/tag1,tag2,tag3/, because I want to load my startpage and use the tags parameter in an angular script.

Does somebody have an idea how I can prevent this redirect?

Upvotes: 2

Views: 1162

Answers (1)

user1383029
user1383029

Reputation: 2125

Found the solution by myself in this thread: https://wordpress.stackexchange.com/questions/184163/how-to-prevent-the-default-home-rewrite-to-a-static-page

Just disable canonical redirect for front page:

function disable_canonical_redirect_for_front_page( $redirect ) {
    if ( is_page() && $front_page = get_option( 'page_on_front' ) ) {
        if ( is_page( $front_page ) )
            $redirect = false;
    }

    return $redirect;
}
add_filter( 'redirect_canonical', 'disable_canonical_redirect_for_front_page' );

Upvotes: 1

Related Questions