Form method = “get” pretty URL

2019-07-22 11:48发布

So I'm using this HTML form for a simple search field

<form action = "/search/" method = "get">
<input type = "text" name = "search_term"/>
</form>

But then when it processes in the URL it's like this

http://mysite.com/search/?search_term=something

I know this is perfectly normal, but is there a way to get it to send like this?

http://mysite.com/search/something

I will then use .htaccess in the search directory to rewrite the url.

2条回答
一纸荒年 Trace。
2楼-- · 2019-07-22 12:12

You would have to use javascript to hook into the submit event of the form, or the click event of some button that submits the form, etc.

You would then redirect the page after constructing the url based on the inputs in the form.

查看更多
3楼-- · 2019-07-22 12:15

Do you want just the URL to be pretty or you want the parameters to be hidden from the URL. You can change your method='post' and it'll pass all the parameters as part of the HTTP Header and completely hidden from your user. If you want to make the URL look nicer, you should look at mod_rewrite and .htaccess files on how to change a search query from ?search_terms=something to /something

查看更多
登录 后发表回答