Need way to RewriteRule in my .htaccess

I translate my website from one language to 3 languages . Now i wanna seo friedly way to change languages.
For example if in my website url’s like ( http://example.com/news , http://example.com/statistic, http://example.com/contacts and other like this)

    RewriteRule ^news$ news.php [L]
    RewriteRule ^statistic$ statistic.php [L]
    RewriteRule ^contact$ contact.php [L]
    RewriteRule ^cur_(usd|eur|nis|rub)$ table.php?currency=$1 [L]

The 3 buttons with languages change will be in top page.
When guest or user click on English the website will be in english, but, the page where is guest or user Now, will be changed from http://example.com/page to http://example.com/page_en . (now my website see lang changes and save info with cookies)

I tried do something like this

RewriteRule (.*)(_en|_fr|_ru)$  $1?lang=$2 [L,QSA]

but i know that is bad, and i need help ))) Thanks and sorry for my bad english.

--------- Edit -----------
It work for me now like this

RewriteRule ^news$ news.php [L]
RewriteRule ^news(_)(en|he|ru)$ news.php?lang=$2 [L]
RewriteRule ^statistic$ statistic.php [L]
RewriteRule ^statistic(_)(en|he|ru)$ statistic.php?lang=$2 [L]
RewriteRule ^contact$ contact.php [L]
RewriteRule ^contact(_)(en|he|ru)$ contact.php?lang=$2 [L]
RewriteRule ^cur_(usd|eur|nis|rub)$ table.php?currency=$1 [L]
RewriteRule ^win_cur_(usd|eur|nis|rub)(_)(en|he|ru)$ table.php?currency=$2&lang=$3 [L]

Hi Alex,

First, please don’t apologize for your language ability because your use of English is far superior to my (and most members) facility with ANY second language.

I remember from quite some time ago that SitePoint uses mod_negotiation to determine the appropriate language to serve members. Personally, I’ve never used it but a quick look at http://httpd.apache.org/docs/current/content-negotiation.html shows that it might solve your problem without resorting to mod_rewrite.

Then, Apache core can use environment statements to store and select language preferences.

Any of the above (1) offer (IMHO) better solutions than mod_rewrite BUT (2) they will require some modification of your scripts.

With that, on to your mod_rewrite question.

Using the language selected by the visitor IN YOUR LINKS is a great way to specify the language as it does not rely on cookies (which many users will prohibit). Your scripts can easily capture the language from the link:

$lang = (isset($_GET['lang']) && ('en' == $_GET['lang'] || 'he' == $_GET['lang'] || 'ru' == $_GET['lang'])) ? $_GET['lang'] : 'en'; // assumes en is the default language

Of course, if you prefer to use cookies, then you can use that code to help set (or reset) the cookie. IMHO, you should allow your visitors to select their language on ANY page so the cookie reset would be important.

Actually, your first attempt (using the :fire: EVERYTHING :fire: atom) is good … but it raises the question of what your scripts use as links. You have used .php scripts in your “it works for me” code so, if you’re using extensionless links, you get kudos for finding the solution. However, I would NOT create atoms containing only the underscore ( the “_” ) like you did for the cur key/value pairs (like below).

Question: Can your scripts handle a null value for lang (default to “en” or other default language as above)? If so, you can easily handle ALL your non-currency scripts in a single RewriteRule:

RewriteRule ^(news|statistic|contact)(_(en|he|ru))?$ $1.php?lang=$3 [L] RewriteRule cur_(usd|eur|nis|rub)(_(en|he|ru))?$ table.php?currency=$1&lang=$3 [L]

Please note that I did NOT test this but null values should still create an atom so the $3 values should work to provide the selected language … and cur_ will also match win_cur_ with the start anchor.

Just be sure to use the $lang value to create your links, e.g. href=“news_<?=$lang?>”.

Regards,

DK

1 Like

Very educational !!! Thank you!

So now best solution will be change languages with subdomains, like this

RewriteCond %{HTTP:Accept-Language} ^ru [NC]
RewriteRule ^$ http://ru.site.ru/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://en.site.ru/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^he [NC]
RewriteRule ^$ http://he.site.ru/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.site\.ru$ [NC]
RewriteRule ^(.*)$ http://site.ru/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.en.site\.ru$ [NC]
RewriteRule ^(.*)$ http://en.site.ru/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.he.site\.ru$ [NC]
RewriteRule ^(.*)$ http://he.site.ru/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.ru.site\.ru$ [NC]
RewriteRule ^(.*)$ http://ru.site.ru/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(ru|en|he)\.site\.ru$
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
RewriteCond %{ENV:LANGUAGE} ^$
RewriteRule (.*) - [QSA,E=LANGUAGE:ru]
RewriteRule (.*) $1?lang=%{ENV:LANGUAGE} [QSA]

It is more simple, and ease. One more question, In the code above is not missing anything? )))

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.