Remove% 5B% 5D from URL when submitting form

When I submit a form with several flags with the same name, I get a URL that looks something like this: www.mysite.com/search.php?myvalue%5B%5D=value1&myvalue%5B%5D=value2

Is there something that I can remove% 5B% 5D to make the URL “cute” with something like htaccess?

the code:

<form> <input type="checkbox" name="myvalue[]" value="value1"> <input type="checkbox" name="myvalue[]" value="value2"> </form> 
+7
source share
2 answers

Is there something that I can remove% 5B% 5D to make the URL “cute” with something like htaccess?

Not. [] reserved characters in URLs, so they must be encoded in the URL .

If using POST is not an option, which makes sense, given that this is a search form, it is best to give them every other name with a value of 1 or so.

 <form> <input type="checkbox" name="option1" value="1" /> <input type="checkbox" name="option2" value="1" /> </form> 

Or, if you really insist that they have the same name, then you should extract the query string yourself instead of relying on the PHP-specific function to return the array when you get the parameter with the suffix [] in the name.

 $params = explode('&', $_SERVER['QUERY_STRING']); foreach ($params as $param) { $name_value = explode('=', $param); $name = $name_value[0]; $value = $name_value[1]; // ... Collect them yourself. } 

That way you can just use the aimless name.

 <form> <input type="checkbox" name="option" value="option1" /> <input type="checkbox" name="option" value="option2" /> </form> 
+14
source

[ and ] are reserved characters in the URL, so the browser must encode them for the URL to work correctly. You cannot have these characters in the url. You also have no other reserved characters, such as spaces, ampersands, etc. All of them will be automatically encoded for you (in many cases, even if you enter the URL in the browser manually).

If you need a "pretty URL", you can:

  • Do not use the form at all; Provide a link to a well-known "pretty" URL.

  • Accept the ugly URL, but redirect it immediately to the pretty URL in step 1 above.

  • Avoid using angle brackets in general in field names (but that would also mean a lot of changes in your inner code)

  • Use the POST method on the form so that these fields are not displayed at all in the URL (but this will mean that you don’t have a link that the user can bookmark).

If you must cover this URL, my suggestion would be option 2 above.

Honestly, I would not worry about that. People get waaaay to emphasize “pretty” URLs. I really don't understand why.

  • Very few people I know have ever typed a URL more than just a domain name.
  • If you are worried about SEO for this, do not do this - search engine bots know what ULR encoding is, and you can view the past.
  • The only reason you need a “pretty” url is good if users share it by email or something like that. To be honest, if you are worried about the sympathy of the URL for this, and it got the form fields in it, then it is already too ugly, with only & and = tags everywhere. Coded brackets do not really make things worse.

So my honest answer is: don't sweat. This is normal; ignore it; tackle the more important parts of your web development work.

+2
source

All Articles