I am trying to create a multilanguage website. And I have set my home page to a static page in my WP admin area.

This home page ID is 2 and it calls front-page.php template when I am on the home page at this address, for example, http://examplesite.com/

So for a french version, I have added a new rewrite rule so I have this url, http://examplesite.com/fr/, and its call the same home page which is ID 2.

add_rewrite_rule(
    '^fr/?$',
    'index.php?&p=2&lang=fr',
    'top'
)

But why it calls index.php template instead but not front-page.php?

How can I make http://examplesite.com/fr/ to call front-page.php?

1 Answer
1

As I pointed in my comment, in the rewrite rule there, change ?&p=2 to ?page_id=2. Because p is used for querying a Post (i.e. post of the post type). So for Pages (i.e. post of the page type), use page_id.

To prevent http://examplesite.com/fr/ from being redirected to http://examplesite.com/, you can cancel (the) canonical redirect, like this:

add_action( 'template_redirect', function(){
    if ( is_front_page() ) {
        remove_action( 'template_redirect', 'redirect_canonical' );
    }
}, 0 );

Leave a Reply

Your email address will not be published. Required fields are marked *