Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Is it possible to remove empty query string parameters from a URL with Apache's mod_rewrite?

A simple PHP script has an HTML form which submits to itself via GET. The HTML form has many input fields and when submitting via GET the resulting URL can end up being incredibly long, for example...
http://www.domain.org/script.php?var1=&var2=&var3=foo&var4= ... &var18=bar

This PHP script only processes query string paramters whose length is > 0. Empty query string params are ignored.

Is anyone aware of a method that can clear the empty variables within the query string so the URL would become?
http://www.domain.org/script.php?var3=foo&var18=bar

I realise I could do this within my PHP script by processing the GET array and building a new query string based on the GET array elements with values then do an HTTP header redirect. But that sounds pretty rubbish.

Is there was a better way of removing empty query string parameters from a URL?

share|improve this question

1 Answer 1

It is possible but very ill-advised. Empty (null) is perfectly legal value for any variable, how will you let the processor know that you want to set some of the variables to empty/null?

share|improve this answer
    
The processing PHP script in question (I assume that's what you are referring to) is a search script which builds an SQL query from the values entered in the form by a user. The PHP script only builds the SQL query with variables that have a length > 0. Empty query string paramters are ignored. This search script actually supports some proprietary search syntax. Including if the user enters an equals sign = in a field, the WHERE clause in the SQL query will be built thus... WHERE var1 IS NULL AND var2 IS NULL ... – bbradley Sep 5 '12 at 23:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.