Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 强制用户';URL重写规则中的语言首选项?_.htaccess_Mod Rewrite - Fatal编程技术网

.htaccess 强制用户';URL重写规则中的语言首选项?

.htaccess 强制用户';URL重写规则中的语言首选项?,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我想在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

我想在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]
******一天后

有人能帮我确切地了解Michael的解决方案在做什么,以及如果我移动到新的子目录,如何更改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


使用
RewriteCond
在URI开头查找两个字母的lang代码:

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个字符,请使用
[a-z]{2,4}

Michael-我尝试了您的解决方案。我得到了“@fnagel,嗯,这是旧的。查看它,可能是因为
REQUEST\u URI
包含一个与条件不匹配的前导
/
。尝试
RewriteCond%{REQUEST\u URI}^/([a-z]{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]