繁体   English   中英

如何在htaccess中重写语言参数?

[英]how to rewrite language parameter in htaccess?

如何在htaccess中重写语言参数?
我想将网址的第二部分设置为网站语言,
我写了下面的htaccess代码,但是当我打印$ _GET时,没有找到$ _GET ['language']
为什么?
顺便说一句,如何使用“?”来判断? 或htaccess中的“&”。 我写了2 RewriteCond波纹管,还有其他简单的方法吗?

http://www.hello.com/en/test.html
or http://www.hello.com/test.html   //default language = en
=> 
http://www.hello.com/test.html?language=en

http://www.hello.com/fr/test.html
=> 
http://www.hello.com/test.html?language=fr

RewriteCond %{REQUEST_URI} ^\w{2}/.*\? [NC]
RewriteRule ^(.*)/(.*) $2&language=$1 [QSA,L]

RewriteCond %{REQUEST_URI} ^\w{2}/[^\?]* [NC]
RewriteRule ^(.*)/(.*) $2?language=$1 [QSA,L]

因为您要捕获参数或编写不存在的参数,所以两个字符串是最简单的方法。 当涉及到mod_rewrite简单并不总是最好的方法。 这应该与QSA 只要您声明新的查询字符串,QSA就会自动传递参数:

#Check for files that do not contain the language string
RewriteCond %{REQUEST_URI} !^/[a-z]{2}/.*
RewriteRule ^(.*)$ $1?language=en [QSA,L]

#Capture the language string and present it as the variable
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/(.*)
RewriteRule ^.* %2?language=%1 [QSA,L]

也许这可行:

# Requests to "/en/test.html" or "/test.html"
RewriteCond %{REQUEST_URI} ^/(?:en)?/?(.*)\.html [NC]
RewriteRule .*              /%1.html?language=en [L]

# Other requests to "/xx/test.html" 
RewriteCond %{REQUEST_URI} !^/en [NC]
RewriteCond %{REQUEST_URI} ^/(\w\w)/(.*)\.html [NC]
RewriteRule .*            /%2.html?language=%1 [L]

要保留传入查询(如果有)附加到替代URL,请用[QSA,L]替换[L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM