I am trying to implement a language prefix to my WordPress. I now have working Rewrite rules for %lang% tag, query_var etc. Everything works ok, except for front page.
What I am trying to do: allow for example.com/de/ to work as a translation for front page. Also allow example.com/ for default language (no translation). But what I struggle with is understanding how to do a Rewrite rule for front page.
So let’s say I have two pages: Page A and Page B. Page A is set as front page via Settings.
What works:
- example.com/de/page-b/
- example.com/page-b/
- example.com/page-a/ – redirects to example.com/
What does not work:
- example.com/de/page-a/ – shows 404 error
- example.com/de/ – shows 404 error
Here is an example of all Rewrite rules in my page, they allow for links with prefix and without:
[(?:(en|fr|de)/)?author/([^/]+)/?$] => index.php?lang=$matches[1]&author_name=$matches[2]
I understand that front pages work differently in WordPress, but cannot understand how to change that. I see that i.e. canonical redirect does a home_url() redirect. But if that is the problem I’m not sure.
I also tried to play with index.php. So i.e. index.php?lang=de shows me an empty page (I thought that empty index.php means its for front page).
After tinkering for a few days, I found out that this works as expected:
add_rewrite_rule('(en|de|fr)/?$', 'index.php?lang=$matches[1]&page_id=1', 'top');
(where page_id=1 is my page ID) as a first Rewrite rule. But problem is that now I have to hardcode page id into a rewrite rule.
Also, I tried playing with redirect_canonical hook, but that does not have a direct connection my problem.
Any ideas? Is there a way to redirect to index.php and also tell WordPress that it should be a front page?