Nice search URLs
I have a form which submits to a search page. I can handle the MOD_REWRITE stuff at the back end but want to know the best way to get the search result page in a nice format in the first place.
Is it best to receive the form submission, then redirect to a nicer url structure. In the followin开发者_Python百科g example the user searches for blah
.
/search.html?searchterm=blah
Which redirects to
/search/blah/
Or can I (my preferred option) do this using javascript (or jQuery) somehow after the user clicks the submit button?
definitely you should read this tutorial:
- http://matthewjamestaylor.com/blog/how-to-post-forms-to-clean-rewritten-urls
basics:
mysql_real_escape_string()
searched term- remove all non Alpha/Num chars
- place the
+
between each matched word SELECT MATCH AGAINST
||IN BOLEAN MODE
||LIKE
mod_rewrite
RewriteRule ^search/([^/\.]+)$ search.php?q=$1
PS: the PHP
part in the link is a little bit outofdate, so, for example you should use preg_replace
instead of ereg_replace
, and you can also avoid the extra step of stripping spaces by doing it all in the first try; you may also check for stopwords
and refine your regex
as suggested ex.: foo-bar. there are other things to consider but for a novice is a good starting point.
From your question I gather you want the browser to go to search/blah directly. You don't want to be forwarded.
The only way to do that is with javascript, since the way a form with method GET works is by definition not that way you want it.
What I would do is leave the form as it is, and use javascript to intercept the button click, then navigate to "search/"+escapedSearchTerm
. This has the advantage that if no javascript is available, the default method still kicks in and the site still works.
The latency should really be neglectable for this.
What search API are you calling?
You should be able to use Javascript or jQuery to pull in search results.
I'd avoid using JavaScript for this. It's not really necessary. I'd probably just leave it simple with search.php?q=blah
, but if clean URLs are important to you, then just rewrite search.html?searchterm=blah
to /search/blah/
like you mentioned.
I don't think there really is a "best" way to format your URL. I guess it boils down to personal preference, unless there are performance issues involved with using mod_rewrite
that I'm not aware of.
I think will be better if the onclick()
action of your submit button changes the action
property of your form to something like /search/blah/
preventing your site's users to see that ugly url.
GET requests are fairly standard for search engines. Having a URL like /search?q=blah is fairly normal and expected behavior and you should be able to accomplish this using mod_rewrite as you suggested.
JavaScript/jQuery is not a good approach for this. It would add additional client-side latency for something that should be handled on the server side (rewrite) especially if the request is properly constructed in the search form.
精彩评论