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?

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

8 Comments

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
Could you please look at my new question here: stackoverflow.com/questions/34548049/…
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
That might be effect of other rules I think. I suggest posting a new question with your problem and I will attend.
This is a real gem! It's a real shame that question like this one doesn't have 100s of upvotes.
|
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]

5 Comments

Thanks but anyphppagehere means: Any PHP Page Name Here, not just specific page.
I fixed the question. You misunderstand my question, It was just an example in my question. I need general code.
@KevinSmith it means "yourPage.php" the page you want to redirect to .
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: example.com/…
@KevinSmith Ok I understand, I will update my answer after 30mnts, actually I am very busy in a party.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.