问题
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/
回答1:
You can use this rule with negation:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} !^(phone|fax|zip)= [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
?
in the end is for stripping out QUERY_STRING from original URL.
来源:https://stackoverflow.com/questions/19728136/rewrite-url-using-htaccess-file-in-case-there-is-used-parameters-which-is-not-i