How to remove empty parameter or parameters anywhere in URL by .htaccess?

不问归期 提交于 2019-12-05 21:12:47

You can use this URL to remove any one or more empty parameters from anywhere in URLs:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,L,NE]

Try :

RewriteEngine on


RewriteCond %{THE_REQUEST} /AnyPHPpageHere\.php\?product=7&keyword=&color=blue&city= [NC]
RewriteRule ^ /AnyPHPpageHere.php?product=7&color=blue [NC,L,R=302]

If values of querystring are dynmic, you can use regex catpure groups :

RewriteEngine on


RewriteCond %{THE_REQUEST} /AnyPHPpageHere\.php\?product=([^&]+)&keyword=&color=([^&]+)&city= [NC]
RewriteRule ^ /AnyPHPpageHere.php?product=%1&color=%2 [NC,L,R=302]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!