3

Empty parameters that I mean can be anywhere in URL each time different place, each time with different name, each time on different php page e.g :

http://www.example.com/AnyPHPpageHere.php?parameter1=7&parameter2=&parameter3=blue&parameter4=

to the form:

 http://www.example.com/AnyPHPpageHere.php?parameter1=7&parameter3=blue

So how to do it via .htaccess?

anubhava
  • 761,203
  • 64
  • 569
  • 643
Kevin Smith
  • 111
  • 7

2 Answers2

2

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • There's an unexpected error when value of my queries contain special characters, like chinese language, ... e.g: 山西省 changed to: %252525E5%252525B1%252525B1%252525E8%252525A5%252525BF%252525E7%2525259C%25252581 – Kevin Smith Dec 31 '15 at 09:59
  • Could you please look at my new question here: http://stackoverflow.com/questions/34548049/301-redirect-old-parameter-names-to-new-element-names-by-htaccess – Kevin Smith Dec 31 '15 at 14:55
  • I need your help sir, there's a complicated to explain problem in your code. I'll try to explain: your code turns off my clean URL setting. before my clean URL was kw.html after adding your code it gets: search.php?color=kw&submit=1 – Kevin Smith Jan 13 '16 at 21:30
  • That might be effect of other rules I think. I suggest posting a new question with your problem and I will attend. – anubhava Jan 13 '16 at 21:36
  • I'll do sir, but before doing it, is there anyway to exclude submit= element in your code, I think this gonna works – Kevin Smith Jan 13 '16 at 21:42
  • try: `RewriteCond %{QUERY_STRING} ^(.+?&)?(?!submit=)[^=]+=(?:&(.*))?$` – anubhava Jan 13 '16 at 21:43
  • new question here: http://stackoverflow.com/questions/34777869/error-with-remove-any-one-or-more-empty-parameters-from-anywhere-in-urls-code – Kevin Smith Jan 13 '16 at 21:58
  • 1
    This is a real gem! It's a real shame that question like this one doesn't have 100s of upvotes. – cezar Feb 19 '18 at 10:11
0

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]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Thanks but anyphppagehere means: Any PHP Page Name Here, not just specific page. – Kevin Smith Dec 30 '15 at 10:45
  • I fixed the question. You misunderstand my question, It was just an example in my question. I need general code. – Kevin Smith Dec 30 '15 at 10:49
  • @KevinSmith it means "yourPage.php" the page you want to redirect to . – Amit Verma Dec 30 '15 at 10:49
  • No, It can be anything, query can be anything, elements can be anything, they also can appear anywhere and each time different places. e.g: http://www.example.com/DifferentPHPpageHere.php?parameter4=&parameter3=blue&parameter1=7&parameter2= – Kevin Smith Dec 30 '15 at 10:54
  • @KevinSmith Ok I understand, I will update my answer after 30mnts, actually I am very busy in a party. – Amit Verma Dec 30 '15 at 11:00