自动将语言前缀添加到url的开头

时间:2012-02-22 01:45:58

标签: .htaccess mod-rewrite

  

可能重复:
  force user's language preference in the URL - rewriterule?

**转发**

我想在URL中指定用户的语言首选项。是否可以使用.htaccess重写规则将一个段添加到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                
>> 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]

在之前的问题中有人建议:

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

但最终也加倍了URL前缀。

0 个答案:

没有答案