2014-07-09 36 views
1

我想将完整的网址(包括QUERY_STRING)重写为index.php?url=[url]&query=[query]。以下规则适用于: RewriteRule ^(.*)$ index.php?url=$1&query=%{QUERY_STRING} [B]B标志确保编码为$1部分,但不编码%{QUERY_STRING}。我怎样才能编码呢?.htaccess encode%{QUERY_STRING}

实施例:

http://www.website.com/test&test.php?arg1=val1&arg2=val2重写到Array('url' => 'test&test.php','query' => 'arg1=val1', 'arg2' => 'val2'),其中Array('url' => 'test&test.php','query' => 'arg1=val1&arg2=val2')预计。

+0

为什么不从PHP获取'query string'并使用[parse_str](http://php.net/manual/en/function.parse-str.php)创建数组? – machineaddict

回答

1

首先,你可以离开现有的查询字符串不变通过QSA标志:

RewriteRule ^(.*)$ index.php?url=$1 [B,QSA,L] 

作为第二个想法,你所带来的查询字符串到规则的第一步,然后捕获它通过B标志来引起正确的编码。我没有测试过,所以尝试这两个选项::

选项1个

RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*) index.php?url=$1&query=%1 [B,L] 

选项2

RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(?!index)(.*) %1URL$1 [B] 
RewriteRule ^(.*?)URL(.*) index.php?url=$1&query=$2 [B,L] 
0

您可以使用此:

RewriteCond %{QUERY_STRING} .* 
RewriteRule .+ index.php?url=$0&query=%0 [B,L] 

我用。+规则中避免由mod_dir(DirectorySlash和DirectoryIndex)造成的双重重写