Rewrite URL using .htaccess file in case there is used parameters which is not in white list

末鹿安然 提交于 2019-12-13 02:37:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!