How to write .htaccess in multilingual site distribution

Asked 2 years ago, Updated 2 years ago, 365 views

We are currently creating a multilingual site with the following configurations.
English version: /en
Japanese version: /ja
Chinese version: /zh

RewriteBase/
RewriteCond%{HTTP:Accept-Language}^ja [NC]
RewriteRule^$/ja/[L,R=301]
RewriteCond%{HTTP:Accept-Language}^zh [NC]
RewriteRule^$/zh/[L,R=301]
RewriteRule^$/en/[L,R=301]

Currently, the above is listed in .htaccess and it works fine, but for example, when I access /book/, I want to skip to /ja/book/, but it will be skipped to /ja/.

RewriteBase/

RewriteCond%{REQUEST_URI}!(^/ja/)
RewriteCond%{HTTP:Accept-Language}^ja [NC]
RewriteRule^(.*)$/ja/$1[L,R=301]

RewriteCond%{REQUEST_URI}!(^/zh/)
RewriteCond%{HTTP:Accept-Language}^zh [NC]
RewriteRule^(.*)$/zh/$1[L,R=301]

RewriteCond%{REQUEST_URI}!(^/en/)
RewriteRule^(.*)$/en/$1 [L,R=301]

"If you write like above, ""/ja/"" and ""/en/"" will result in an infinite loop."
What should I do to prevent it from becoming an infinite loop?

apache .htaccess

2022-09-30 22:04

2 Answers

RewriteCond%{REQUEST_URI}!(^/en/)

This is probably because /ja/~ and /zh/~ will also match.How about ending the match for the three redirected languages first?

RewriteBase/

RewriteCond%{REQUEST_URI}^/(ja|zh|en)/
RewriteRule.* - [L]

RewriteCond%{HTTP:Accept-Language}^ja [NC]
RewriteRule^(.*)$/ja/$1[L,R=301]

RewriteCond%{HTTP:Accept-Language}^zh [NC]
RewriteRule^(.*)$/zh/$1[L,R=301]

RewriteRule^(.*)$/en/$1 [L,R=301]


2022-09-30 22:04

Thank you for your reply, it has been resolved.
However, according to what you suggested, it seems to be an infinite loop.

Although it is redundant, it has been resolved as follows.

RewriteCond%{REQUEST_URI}!^/(ja|zh|en)/
RewriteCond%{HTTP:Accept-Language}^ja [NC]
RewriteRule^(.*)$/ja/$1[L,R=301]
RewriteCond%{REQUEST_URI}!^/(ja|zh|en)/
RewriteCond%{HTTP:Accept-Language}^zh [NC]
RewriteRule^(.*)$/zh/$1[L,R=301]
RewriteCond%{REQUEST_URI}!^/(ja|zh|en)/
RewriteRule^(.*)$/en/$1 [L,R=301]


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.