如何在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]
答案 0 :(得分:1)
两个字符串是最简单的方法,因为您要么捕获参数,要么编写不存在的参数。说到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]
答案 1 :(得分:0)
也许这有效:
# 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]
要保留传入查询(如果有)附加到替换网址,请将[L]替换为[QSA,L]