簡體   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