在 .htaccess 中处理语言
Handle languages in .htaccess
我要解决下一个问题:
1) 无语言输入url时,默认使用英文
示例:
http//localhost/plants/ or http//localhost/plants
或
http//localhost/plants/shop/accessories
2) 当输入 url with language 时,将该参数作为要使用的语言传递
示例:
http//localhost/plants/es/shop/accessories
或
http//localhost/plants/es/ or http//localhost/plants/es
到目前为止我已经尝试过:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=&lang= [L,QSA]
RewriteRule ^(.*)$ index.php?url=&lang=en [L,QSA]
如果我评论第一个 RewriteRule 并输入任何 url 它工作正常但总是使用 en 语言。
如果我评论第二个 RewriteRule 并使用 url 语言:
http//localhost/plants/es/
或
http//localhost/plants/es/shop/accessories
它工作正常,但是在没有给定的情况下它不会将默认语言设置为英语。
知道为什么当我离开这两个规则时它不起作用吗?
谢谢
Ps:我删除了 http
之后的 :
将以下 htaccess 文件放入 /plants
目录:
RewriteEngine On
RewriteBase /plants
RewriteCond %{REQUEST_URI} !^/plants/e[ns](/|$) [NC]
RewriteCond %{QUERY_STRING} !lang=e[ns] [NC]
RewriteRule ^.*$ /plants/en/[=10=] [R=301,L]
RewriteRule ^(e[ns])/(.*)$ index.php?url=&lang= [NC,L,QSA]
如果上述方法仍然无效,请将您自己的方法更新为:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(en|es)/(.*)$ index.php?url=&lang= [L,QSA]
RewriteRule ^(.*)$ index.php?url=&lang=en [L,QSA]
找到解决方案:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=&lang= [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=&lang=en [L,QSA]
我该如何改进?感觉不对我必须写twto时间一样Cond
如果 plants
是您的文档根目录,您应该可以使用此规则。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !/(en|es)
RewriteRule ^(.*)/?$ index.php?url=&lang=en [L,QSA]
RewriteRule ^(en|es)/?(.*)/?$ index.php?url=&lang= [L,QSA]
告诉我它是如何为你工作的。
我要解决下一个问题:
1) 无语言输入url时,默认使用英文 示例:
http//localhost/plants/ or http//localhost/plants
或
http//localhost/plants/shop/accessories
2) 当输入 url with language 时,将该参数作为要使用的语言传递 示例:
http//localhost/plants/es/shop/accessories
或
http//localhost/plants/es/ or http//localhost/plants/es
到目前为止我已经尝试过:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=&lang= [L,QSA]
RewriteRule ^(.*)$ index.php?url=&lang=en [L,QSA]
如果我评论第一个 RewriteRule 并输入任何 url 它工作正常但总是使用 en 语言。
如果我评论第二个 RewriteRule 并使用 url 语言:
http//localhost/plants/es/
或
http//localhost/plants/es/shop/accessories
它工作正常,但是在没有给定的情况下它不会将默认语言设置为英语。
知道为什么当我离开这两个规则时它不起作用吗?
谢谢
Ps:我删除了 http
之后的 :将以下 htaccess 文件放入 /plants
目录:
RewriteEngine On
RewriteBase /plants
RewriteCond %{REQUEST_URI} !^/plants/e[ns](/|$) [NC]
RewriteCond %{QUERY_STRING} !lang=e[ns] [NC]
RewriteRule ^.*$ /plants/en/[=10=] [R=301,L]
RewriteRule ^(e[ns])/(.*)$ index.php?url=&lang= [NC,L,QSA]
如果上述方法仍然无效,请将您自己的方法更新为:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(en|es)/(.*)$ index.php?url=&lang= [L,QSA]
RewriteRule ^(.*)$ index.php?url=&lang=en [L,QSA]
找到解决方案:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=&lang= [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=&lang=en [L,QSA]
我该如何改进?感觉不对我必须写twto时间一样Cond
如果 plants
是您的文档根目录,您应该可以使用此规则。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !/(en|es)
RewriteRule ^(.*)/?$ index.php?url=&lang=en [L,QSA]
RewriteRule ^(en|es)/?(.*)/?$ index.php?url=&lang= [L,QSA]
告诉我它是如何为你工作的。