mod rewrite search form GET vs POST ACTION
Hi there and thanks for taking the time for this question.
I'm using a search form using jQuery for live searches. The search bar is on every page on my website and works fine. Now I want to extend it. When people hit the Enter key I want them to be redirected to the search page where they get all the results.
The only problem is that the Get Method is not working. My form is looking like this:
<form action="search/search" id="Searchform" method="GET">
<p><input type="text" name="SearchInput" id="SearchInput" value="" onkeyup="lookup(this.value);" /></p>
<div class="clear"></div>
<div id="suggestions"></div>
</form>
My .ht开发者_JAVA技巧access file contains this rewrite rule for the search page:
RewriteRule ^search/([^/]*)$ search.php?mode=$1 [L]
Evey time I hit the Enter button I get this:
search/search?SearchInput=moonwalker
And using the GET method I can't seem to get the value of SearchInput at all.
When using the POST method everything works fine. But I read in different articles that I really should be using GET method for searches.
So my question is: Why should I use the GET method for search pages? Are there any big advantages if I just decide to use POST instead of the GET method?
I know I can use redirects etc to accomplish this with the GET method, but I just want to know why POST is considered bad practice in search forms.
Thanks in advance for your help!
You need to re-append the query string:
RewriteRule ^search/([^/]*)$ search.php?mode=$1 [L,QSA]
The QSA above does the magic. Otherwise, everything in the URL after the ?
is truncated and replaced by the new query string ?mode=foo
.
To answer the question: It is good practice for several reasons to do non-transforming queries with GET (everything that just creates a view) and changing requests (like, posting a comment or a new product) with POST. Eric Petroelje gives some reasons in his answer. Some others can be found, when you google for REST + GET + POST. (This leads, e.g., to this SO question)
A couple reasons off the top of my head why you want to use GET for search screens:
- It allows people to bookmark/link to a search, or send a search to someone else.
- It allows searches to be indexed by Google. Googlebot won't submit POST forms.
精彩评论