使用htaccess从URL中删除某些查询字符串值(但不是全部)

时间:2011-12-09 10:44:00

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

作为上一篇文章的扩展,我有以下网址:

test/example/example/index.html?t=Audi&p=Petrol

但是我想要一个重写规则来删除查询字符串 t p 。请注意,可能存在其他可传递的查询字符串值,这些值不应被剥离。

2 个答案:

答案 0 :(得分:2)

尝试:

# First remove the t=something
RewriteCond %{QUERY_STRING} ^(.*)(^|&)t=[^&]+(.*)$
RewriteRule ^(.*)$ /$1?%1%3 [N]

# Next remove the p=something
RewriteCond %{QUERY_STRING} ^(.*)(^|&)p=[^&]+(.*)$
RewriteRule ^(.*)$ /$1?%1%3 [L]

所以转到http://domain/test/example/example/index.html?a=b&t=fooo&d=f&p=barr&e=r会导致URI被重写为:/test/example/example/index.html?a=b&d=f&e=r

答案 1 :(得分:0)

这样的事情:

RewriteRule ^/(.*)\&t=[a-zA-Z]+(.+)$ %1%2 [L]

如果你需要收集那些参数,也可以使用一些RewriteCond,即。虽然是ENV。