mod_rewrite将字符串追加到查询中

时间:2012-01-08 23:59:59

标签: apache url mod-rewrite redirect

我正在尝试转换此网址:

http://localhost/?q=foo&s=search

就此:

http://localhost/?q=+foo&s=search (i add a plus sign before the user query)

我尝试过:

RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^(.*)$ /?q=\+%1 [R=301,L,NE]

但这会产生:

http://localhost/?q=+++++++++++++++++++++foo

有关如何在查询前添加加号的任何建议吗?

1 个答案:

答案 0 :(得分:0)

两个快速/简单的选择:

  1. 在后端添加+(PHP:$query = '+' . $_GET['q'];
  2. 添加一个指示重定向的参数并避免重定向。

    # The example is untested, but the idea is to add an extra 
    # query parameter (rw=1). When the redirect comes back through, 
    # the redirect is not applied, as the '+' as already been 
    # put on the beginning.
    
    RewriteCond %{QUERY_STRING} !&rw=1$ [NC]
    RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
    RewriteRule ^(.*)$ /?q=\+%1&rw=1 [R=301,L,NE]