力用户的语言preference在URL - 重写规则? [英] force user's language preference in the URL - rewriterule?

查看:169
本文介绍了力用户的语言preference在URL - 重写规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要指定用​​户的语言preference在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
 

如果语言不存在,我想自动变换链接直接默认为英语。

  mysite.com/directory
>>应转化为mysite.com/directory/en~~V

mysite.com/directory/about_us.php
>>应转化为mysite.com/directory/en/about_us.php~~V
 

例如:

  RewriteEngine叙述上

#不重定向

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


重写规则^!(FR | EN)/ / ... ...的东西

#重定向
重写规则^(。+)$的index.php?URL = $ 1 [QSA,L]
 

的**的 * 的**的 一天之后的 的**的 * **的 * 的****

有人可以帮助我了解正是迈克尔的解决方案正在做,以及如何改变htaccess的,如果我移动到新的子目录

  mysite.com/directory
>>应转化为mysite.com/directory/en~~V

mysite.com/directory/subdirectory/about_us.php
>>应转化为mysite.com/directory/en/subdirectory/about_us.php~~V
 

...

 #如果网址不匹配/ XX / otherstuff
!#^([AZ] {2}) - 整整两个字母字符不是在字符串的开头
#$(*) - 存储上述发现为$ 1的结果
的RewriteCond%{REQUEST_URI}!^([A-Z] {2} /)(。*)$

#重写到/ EN /
#^(。+)$  - 开始
#HTTP://%{HTTP_HOST} / EN / $ 1  - 取代以http://主机名/ EN
#L =最后一站,立即重写过程中,不施加任何更多的规则。
#R =重定向强制外部重定向,可选择与指定的HTTP状态code
#QSA  - 查询字符串追加 - 强制重写引擎追加替换字符串的查询字符串部分,以现有的字符串
重写规则^(+)$的http://%{HTTP_HOST} / EN / $ 1 [L,R,QSA]
 

解决方案

使用的RewriteCond 寻找2信郎code的URI的开始

  RewriteEngine叙述上

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
#如果网址不匹配/ XX / otherstuff
的RewriteCond%{REQUEST_URI}!^([A-Z] {2} /)(。*)$
#重写到/ EN /
重写规则^(+)$的http://%{HTTP_HOST} / EN / $ 1 [L,R,QSA]
 

如果您使用的是2个以上的字符郎codeS一样,最多4个,使用 [AZ] {2,4} 代替。

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]

解决方案

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.

这篇关于力用户的语言preference在URL - 重写规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆