htaccess抓取其他参数并将它们传递给重定向点

时间:2011-12-30 11:12:06

标签: apache .htaccess mod-rewrite url-rewriting

嘿,我已经在项目上进行了不少重写,但现在我希望能够将其他参数传递给主要接入点

我的htaccess是:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^$                        index.php  [L]
RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteRule ^([^/]+)/?([^/]*)/?/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)$ index.php?account=$1&task=$2&object_id=$3&app_type=$4&p=$5&items=$6

我想要的是能够传递额外的参数,这些参数应该被重写并传递给index.php以及原始参数,如:

http://site.com/user/books/434?another_param=8989898

并且应该相应地重写为index.php,如下所示:

index.php?account=user&task=books&object_id=434&app_type=&p=&items=&another_param=8989898

我必须对.htaccess进行哪些修改才能重写这些附加参数并将它们附加到正在进行的原始重定向中?

提前致谢。

2 个答案:

答案 0 :(得分:1)

您需要添加QueryStringAppend [QSA]标志,例如:

RewriteRule ^([^/]+)$ index.php?rt=$1 [L,QSA] 

答案 1 :(得分:1)

死简单,添加" Query-String-Append"标记为rewriteRule,格式为:

RewriteRule Pattern Substitution [flags]

(来源)http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

query-string-append的标志只是 QSA ,因此在您的示例中您将拥有:

RewriteRule ^([^/]+)/?([^/]*)/?/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)$ index.php?account=$1&task=$2&object_id=$3&app_type=$4&p=$5&items=$6 [QSA]

另一个有用的标志是 L ,这意味着" Last"即,apache在执行替换后停止寻找更多匹配:

RewriteRule ^([^/]+)/?([^/]*)/?/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)$ index.php?account=$1&task=$2&object_id=$3&app_type=$4&p=$5&items=$6 [QSA,L]