在 URL 中强制用户的语言首选项 - 重写规则?

发布于 2025-01-07 06:56:19 字数 1977 浏览 1 评论 0原文

我想在 URL 中指定用户的语言首选项。 如果缺少的话,是否可以使用 .htaccess 重写规则将段添加到 url 中。

URL 通常应具有此结构

mysite.com/directory/en
mysite.com/directory/fr
mysite.com/directory/en/about_us.php
mysite.com/directory/fr/about_us.php

如果缺少语言,我想自动将 url 转换为默认英语。

mysite.com/directory                
>> should be transformed to mysite.com/directory/en 

mysite.com/directory/about_us.php   
>> should be transformed to mysite.com/directory/en/about_us.php

示例:

RewriteEngine On 

# don't redirect 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l


RewriteRule !^(fr|en)/ /... something...

# redirect 
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

*****一天后**********

有人可以帮助我准确理解迈克尔的解决方案是什么吗如果我移动到新的子目录,如何更改 htaccess

mysite.com/directory                
>> should be transformed to mysite.com/directory/en 

mysite.com/directory/subdirectory/about_us.php   
>> should be transformed to mysite.com/directory/en/subdirectory/about_us.php

...

# If the URL doesn't match /xx/otherstuff
# !^([a-z]{2}) - exactly two alpha characters not at the beginning of the string
# (.*)$ - store the result found above as $1
RewriteCond %{REQUEST_URI} !^([a-z]{2}/)(.*)$

# Rewrite to /en/
# ^(.+)$ - begins with 
# http://%{HTTP_HOST}/en/$1 - replace with http://hostname/en
# L = Last Stop the rewriting process immediately and don't apply any more rules.
# R = Redirect Forces an external redirect, optionally with the specified HTTP status    code
# QSA - Query String Append - forces the rewrite engine to append a query string part of the substitution string to the existing string
RewriteRule ^(.+)$ http://%{HTTP_HOST}/en/$1 [L,R,QSA]

I want to specify the user's language preference in the URL.
Is it possible to use .htaccess rewrite rule to add a segment to the url, if missing.

URL should normally have this structure

mysite.com/directory/en
mysite.com/directory/fr
mysite.com/directory/en/about_us.php
mysite.com/directory/fr/about_us.php

If language is missing, I want to automatically transform the url to default to english language.

mysite.com/directory                
>> should be transformed to mysite.com/directory/en 

mysite.com/directory/about_us.php   
>> should be transformed to mysite.com/directory/en/about_us.php

Example:

RewriteEngine On 

# don't redirect 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l


RewriteRule !^(fr|en)/ /... something...

# redirect 
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

***** one day later **********

can someone help me understand exactly what Michael's solution is doing and how to change htaccess if I move to new subdirectory

mysite.com/directory                
>> should be transformed to mysite.com/directory/en 

mysite.com/directory/subdirectory/about_us.php   
>> should be transformed to mysite.com/directory/en/subdirectory/about_us.php

...

# If the URL doesn't match /xx/otherstuff
# !^([a-z]{2}) - exactly two alpha characters not at the beginning of the string
# (.*)$ - store the result found above as $1
RewriteCond %{REQUEST_URI} !^([a-z]{2}/)(.*)$

# Rewrite to /en/
# ^(.+)$ - begins with 
# http://%{HTTP_HOST}/en/$1 - replace with http://hostname/en
# L = Last Stop the rewriting process immediately and don't apply any more rules.
# R = Redirect Forces an external redirect, optionally with the specified HTTP status    code
# QSA - Query String Append - forces the rewrite engine to append a query string part of the substitution string to the existing string
RewriteRule ^(.+)$ http://%{HTTP_HOST}/en/$1 [L,R,QSA]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2025-01-14 06:56:19

使用 RewriteCond 查找 URI 开头的 2 个字母语言代码:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If the URL doesn't match /xx/otherstuff
RewriteCond %{REQUEST_URI} !^([a-z]{2}/)(.*)$
# Rewrite to /en/
RewriteRule ^(.+)$ http://%{HTTP_HOST}/en/$1 [L,R,QSA]

如果您使用 2 个以上字符语言代码(例如最多 4 个),请使用 [az]{2 ,4} 相反。

Use a RewriteCond to look for the 2 letter lang code at the beginning of the URI:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If the URL doesn't match /xx/otherstuff
RewriteCond %{REQUEST_URI} !^([a-z]{2}/)(.*)$
# Rewrite to /en/
RewriteRule ^(.+)$ http://%{HTTP_HOST}/en/$1 [L,R,QSA]

If you are using more than 2 character lang codes, like up to 4, use [a-z]{2,4} instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文