I am building a Wordpress website with two language support English and Danish.
I want to keep the language code string en
for English and da
for Danish prepended in request uri.
Like: (Currently this is working for me)
http://example.com/da
If i visit post or page, it should be map like this: (This is not working, getting 404)
http://example.com/da/post-name
http://example.com/da/page-name
http://example.com/da/post/is/too/long
I have also tried Wordpress Rewrite API
add_rewrite_rule()
(Rewrite rules currently i have)
<?php
add_action('init', function () {
add_rewrite_rule(
'^(da|en)/?', //Regex
'index.php?lang=$matches[1]', //request to
'top' //called earlier than wordpress rules
);
});
and also add_rewrite_tag()
, but i think Wordpress just provide an add_rewrite_endpoint
(and i don't need this at all).
I think it may only be possible with htaccess %{QUERY_STRING}
conditions? (Don't know)
.htaccess contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Edit:
I'm using WP Native Dashboard for translation on admin pages however on front i'm just using __()
and _e()
with .mo
and .po
files and its working perfectly.
P.S:
This problem is not specific to Wordpress website, I also need this help with custom based websites in future. Provide me .htaccess rules/conditions if you can.