2016-04-19 62 views
0

我正在使用mod_rewrite提供漂亮的URL的cms。基本用例只是需要任何路径,并重写个人GET参数,就像这样:使用mod_rewrite添加基于GET的搜索参数到url

RewriteRule ^([\w-]+)[/]?$ index.php?p1=$1 [L] 
RewriteRule ^([\w-]+)/([\w-]+)[/]?$ index.php?p1=$1&p2=$2 [L] 
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)[/]?$ index.php?p1=$1&p2=$2&p3=$3 [L] 
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)[/]?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L] 
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)[/]?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L] 

我也有发送GET请求搜索形式,但我不知道如何将它添加到重写规则。例如:

http://site.domain/search?query=abc

应改写为:

http://site.domain/index.php?p1=search&query=abc

我曾尝试以下,有一些变化,但作为expexted搜索参数不会出现在$ _GET:

RewriteRule ^search?(.+)[/]?$ index.php?p1=search$1[L] 

回答

2

更改全部[L][L,QSA]

QSA | qsappend当更换URI包含查询字符串,重写规则的 默认行为是放弃现有的查询字符串 ,并与新生成的更换。使用[QSA] 标志会导致查询字符串被合并。 http://httpd.apache.org/docs/2.2/rewrite/flags.html

+1

谢谢,这工作像一个魅力! – cornergraf

+0

不客气,很高兴它解决了。 – Croises