简体   繁体   中英

how to rewrite language parameter in htaccess?

how to rewrite language parameter in htaccess?
I want to set the second part of my url as my site language,
I write the htaccess code bellow but when I print $_GET, found no $_GET['language']
why?
by the way , how to juddge using "?" or "&" in htaccess . I wrote 2 RewriteCond bellow , is there other simpple way?

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]

Two strings is the simplest way since you're either capturing parameters or writing non-existent ones. When it comes to mod_rewrite simple isn't always the best method. This should work with QSA . As long as you're declaring a new query string QSA should pass the parameters automatically:

#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]

Maybe this works:

# 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]

To keep the incoming query, if any, appended to the substitution URL, replace [L] with [QSA,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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