使用情况.htaccess文件URL重写有使用的参数是不是白名单(Rewrite URL using

2019-10-18 22:03发布

Need to achieve the following: Rewrite URL using .htaccess file in case there is used parameters which is not in white list.

Example: Whitelisted parameters: phone, fax, zip

Input URL:     http://hostname/adress?phone=1234567890
Resulting URL: http://hostname/adress?phone=1234567890

Input URL:     http://hostname/contacts?fax=1234567
Resulting URL: http://hostname/contacts?fax=1234567

Input URL:     http://hostname/test?zip=1234
Resulting URL: http://hostname/test?zip=1234

Input URL:     http://hostname/test?phoneHack=1234567890
Resulting URL: http://hostname/test

Input URL:     http://hostname/mytest?anotherParam=1234567890
Resulting URL: http://hostname/mytest

So far my findings:

RewriteCond %{QUERY_STRING} ^(phone|fax|zip)
RewriteRule .* http://hostname/%{REQUEST_URI}?%{QUERY_STRING}

Tool where to test: http://martinmelin.se/rewrite-rule-tester/

Answer 1:

您可以使用否定这条规则:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} !^(phone|fax|zip)= [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]

? 到底是从原来的URL剥离出来QUERY_STRING。



文章来源: Rewrite URL using .htaccess file in case there is used parameters which is not in white list