在 URL 中强制用户的语言首选项 - 重写规则?
我想在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
RewriteCond
查找 URI 开头的 2 个字母语言代码:如果您使用 2 个以上字符语言代码(例如最多 4 个),请使用
[az]{2 ,4}
相反。Use a
RewriteCond
to look for the 2 letter lang code at the beginning of the URI:If you are using more than 2 character lang codes, like up to 4, use
[a-z]{2,4}
instead.