5

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.

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
  • Could you please check if you have generated .htaccess file and if yes - copy it's content here, thanks! – Minister Jan 21 '14 at 11:40
  • @Minister Its same as wordpress default generated htaccess. By the way see my edit! – Rahil Wazir Jan 21 '14 at 11:43
  • Thanks, it seems a single WP install, not multisite! – Minister Jan 21 '14 at 11:49
  • Yes it is single WP site! – Rahil Wazir Jan 21 '14 at 11:52
  • Another question - Do you see the WP 404 page not found or you see the web server's default 404 page? – Minister Jan 21 '14 at 12:49
  • I see the wordpress default 404 – Rahil Wazir Jan 21 '14 at 12:55
  • Try the suggestion of Hüseyin. It seems like all is working as expected according to your very last comment: "Right now i'm manually inserting en/da in url..." (Sometimes you need to disable and enable a plugin to get it to work properly! You can try it later...) – Minister Jan 21 '14 at 13:02
  • Do you actually _have_ en/ and da/ directories? If not, are en and da supposed to be converted to ?lang=en or ?lang=da in the .htaccess file (and removed from the URI)? – Phil Perry Jan 21 '14 at 14:49
  • @PhilPerry I don't have directories. Yes they are query strings and need to be converted to `/en/` or `/da/` – Rahil Wazir Jan 21 '14 at 14:55
  • No, you are not converting _to_ SEO format, you are converting _from_ SEO format (fake directories --> URL Query String). Once you have it in the format `?lang=XX`, 1) the server will be able to find the proper directories, and 2) PHP will know the language variable. If WP isn't providing this built-in, what have you tried to use in .htaccess? – Phil Perry Jan 21 '14 at 14:58
  • @PhilPerry BTW this issue has been solved i don't want to start over again. – Rahil Wazir Jan 21 '14 at 15:00

3 Answers3

4

If I were you, I would use plugin instead of implementing from zero. You can use this plugin for multilingual wp site. This plugin provides you three types of url structure;

  1. ?lang=en
  2. /en/foo/
  3. en.yoursite.com

If you want to use for custom site, you can use following rewrite rule;

RewriteEngine On
RewriteBase /
RewriteRule ^(en|da)/(.*)$ /$2?language=$1 [QSA,L]

I assume, you are using language param for language

Edit: There is some bugs on qTranslate plugin. That bugs can be solvable with additional plugin called qTranslate Slug. Do not forget to use this additional plugin, if you faced url pattern problems

Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
  • See my updated question please. qTranslate plugin is not what i'm looking for i have no issue with translation, only with url mapping. By the way your rule is not working with `/en/post/is/too/long` with long urls?? – Rahil Wazir Jan 21 '14 at 12:21
  • 1
    qTranslate provides you three types of url mapping – Hüseyin BABAL Jan 21 '14 at 12:23
  • Yes this works great but heres a problem if click other links my language code dissappear i want to keep the language code in url? Right now i'm manually inserting `en/da` in url any idea? – Rahil Wazir Jan 21 '14 at 12:39
  • 1
    Could you please install this addon http://wordpress.org/plugins/qtranslate-slug/ for fix your case? I have forgot to mention about this case – Hüseyin BABAL Jan 21 '14 at 12:46
  • It worked for me please edit your answer for suggestion of qtranslate-slug plugin, so others will see it. – Rahil Wazir Jan 21 '14 at 14:35
  • @HüseyinBABAL I'm happy the issue has been resolved (+1), I just want to make it clear for me, please. I didn't understand does the Rahil use WP Native Dashboard and needs to use Qtranslate slug to work properly or he also uses the Qtranslate plugin and this is the reason for the need of Qtranslate slug? Thanks in advance! – Minister Jan 21 '14 at 15:42
  • 2
    Also uses qtranslate. Slug is for fixing. Some. Bugs – Hüseyin BABAL Jan 21 '14 at 15:45
  • Thanks! Could be useful (at least to me) to avoid using qtranslate, which usually depends on the client's requirements! Thanks again! – Minister Jan 22 '14 at 04:41
0

I deleted my answer as it seems the plugin should handle the rewritten URLs like example.com/da/post-name...

At the moment the WP is loaded when you visit any URL and the "WP 404 error" is shown.

EDIT 3:

It seems like all is working as expected according to your very last comment: "Right now i'm manually inserting en/da in url..."

Minister
  • 1,198
  • 2
  • 10
  • 18
0

I hope you have to check your server setting if rewrite module inactive. From your server keep rewrite module active.

Anjana
  • 469
  • 3
  • 9