清理搜索查询的 URL? [英] Clean URLs for search query?

查看:14
本文介绍了清理搜索查询的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

HTML

<a href="/search/querystring">query</a>

htaccess

RewriteRule ^search/([-0-9a-z]+)$ /search.php?q=$1 [L]

<小时>

浏览搜索表单:


Going through a search form:

<form method="get" action="/search">
<input type="search" name="q" value="querystring" />
<input type="submit" />
</form>

  • 结果:http://example.com/search?q=querystring
  • 预期结果:http://example.com/search/querystring
  • 这可以用 htaccess 还是我需要从 search.php 中用 PHP 重定向?

    所需结果示例:http://twitter.com/search/hello

    编辑

    我不想依赖 JavaScript 来执行此操作,因此搜索引擎和禁用 JavaScript 的人也会看到这一点.

    I prefer not to be dependant on JavaScript to do this so search engines and folks with JavaScript disabled will see this too.

    推荐答案

    我认为问题在于您使用 GET 方法创建了一个 HTML 表单,该表单会自动以您指定的方式打开 URL 作为结果.如果你想提交你想要的搜索查询,你应该用一些 JavaScript 修改表单,像这样调用你漂亮的 URL:

    I think the problem is that you've created an HTML form with GET method, which automatically opens the URL that way you specified as the result. If you want to submit your search query like the desired one, you should hack the form with some JavaScript to call your good-looking URL like this:

    <form method="get" action="/search/" onsubmit="return false;">
    <input type="search" name="q" value="querystring" />
    <input type="submit" onclick="window.location.href=this.form.action + this.form.q.value;" />
    </form>
    

    这篇关于清理搜索查询的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆