Use .htaccess to give form submissions clean URLs
I'd like to rewrite form submissions to clean URLs, so that examp开发者_如何学Gole.com/search/go?term=foo
becomes example.com/search/foo
How do I do this?
Use JavaScript:
<input type="text" id="search_term" />
<input type="button" value="Search" onclick="goSearch()" />
<script type="text/javascript">
function goSearch() {
var term = document.getElementById("search_term");
window.location = "http://example.com/search/" + escape(term);
}
</script>
URL encode the query string, and redirect to the desired path.
You can change the URL that is called in the action attribute of the form element in HTML. Alternatively, you could set it in an OnClick JavaScript event handler on a button.
You don't need mod_rewrite and .htaccess to accomplish this.
Though, you could use mod_rewrite to redirect everything that's not a document to an index controller script and do the translation there.
RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
That's how Zend Framework does it, and actions look like example.com/search/foo
instead of example.com/search/go?term=foo
. Of course, search
would need to be a script.
精彩评论