htaccess只重写部分查询字符串

时间:2010-06-24 14:36:04

标签: .htaccess redirect

我正在尝试在htaccess中执行以下规则:

 www.domain.com/folder/?id=14077&c=en-gb    ->  www.domain.com/folder/?id=14077 
 www.domain.com/folder/?c=en-gb&ID=14077    ->  www.domain.com/folder/?id=14077 
 www.domain.com/folder/?id=14077&c=fr-fr    ->  www.domain.fr/folder/?id=14077 
 www.domain.com/folder2/?c=fr-fr&ID=14077   ->  www.domain.fr/folder2/?id=14077
 www.domain.com/folder2/?c=en-us&ID=14077   ->  www.domain.us/folder2/?id=14077 

基本上取出查询字符串的“c”部分,并根据以下规则将其重定向到新域:

 c=en-gb -> www.domain.com
 c=fr-fr -> www.domain.fr
 c=en-us -> www.domain.us

欢迎任何帮助!

1 个答案:

答案 0 :(得分:1)

不可否认,我不确定这是mod_rewrite最好解决的任务......但是,为什么不呢:

(未完全测试,但似乎效果很好)

RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)(\A|&)c=([A-Za-z\-]+)&?(&.*)?$
RewriteRule .* - [E=SWITCHLANG:%3,E=QSONE:%1,E=QSTWO:%4]

RewriteCond %{ENV:SWITCHLANG} =en-gb [NC]
RewriteCond %{HTTP_HOST} !(.*)\.com$
RewriteRule (.*) http://www.domain.com/$1?%{ENV:QSONE}&%{ENV:QSTWO}

RewriteCond %{ENV:SWITCHLANG} =fr-fr [NC]
RewriteCond %{HTTP_HOST} !(.*)\.fr$
RewriteRule (.*) http://www.domain.fr/$1?%{ENV:QSONE}&%{ENV:QSTWO}

RewriteCond %{ENV:SWITCHLANG} =en-us [NC]
RewriteCond %{HTTP_HOST} !(.*)\.us$
RewriteRule (.*) http://www.domain.us/$1?%{ENV:QSONE}&%{ENV:QSTWO}