0
votes

I work on a website that has two languages: English and French.

  • Pages available in English are under /en in URL
  • Pages available in French are under /fr in URL

When a user arrives on the homepage, he is redirected to www.example.com/fr if its browser is in French, otherwise he is redirected to www.example.com/en.

Not all the pages are available in both languages, so there isn't any language redirection on other pages than root.

But I have few pages that I'd like to be redirected according to language. How can I redirect www.example.com/page to www.example.com/en/page (or www.example.com/fr/page)?

UPDATE: This is the code we use in .htaccess to redirect the home page:

RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^/?$ /fr [R=302,L]
1
How are you currently doing the redirection for the home page? The redirection for the "page" might just require a small addition to the current method? - DocRoot
To redirect the home page, this is what I do in my .htaccess: RewriteCond %{HTTP:Accept-Language} ^fr [NC] RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC] RewriteRule ^/?$ /fr [R=302,L] - Aliz
You should edit your question to add the relevant additional information - this will also help to retain its formatting. - DocRoot
Is the check against HTTP_HOST necessary? Do you host multiple domains on this account? - DocRoot

1 Answers

1
votes

You can %{HTTP: Accept-Language} variable to detect language of a Web browser.

RewriteEngine on
#if browser language is "en"
RewriteCond %{HTTP: Accept-Language} ^en
#redirect /page to /en/page
RewriteRule ^((?!en).+)$ /en/$1 [L,R]
#if browser language is "fr"
RewriteCond %{HTTP: Accept-Language} ^fr
#redirect /page to /fr/page
RewriteRule ^((?!fr).+)$ /en/$1 [L,R]