url重写/格式化和php搜索表单

时间:2017-01-02 01:03:30

标签: .htaccess url mod-rewrite search url-rewriting

新年快乐!,我在php中有一个搜索表单,即时通讯使用mod_rewrite,现在,我想要的是这个:

我的正常网址: http://mywebsite.com/buscar.php?p=my+search+string&pag=1

TO:

mod重写: http://mywebsite.com/buscar/my+search+string/1

我怎么能这样做?

的.htaccess

RewriteEngine On RewriteRule ^buscar/(^[a-zA-Z0-9[:space:]]*$)/([0-9]+) buscar.php?p=$1&pag=$2

HTML表单

<form action="buscar/1/" method="GET">
<div id="custom-search-input">
    <div class="input-group col-md-12">
        <input type="text" name="p" class="search-query form-control" placeholder="Buscar..." required autocomplete="off"/>
        <span class="input-group-btn">
            <button class="btn btn-danger" type="submit">
                <span class="glyphicon glyphicon-search"></span>
            </button>
        </span>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

你必须使用javascript。

您可以使用此功能:

function dosearch(frm) {
  var text = document.getElementsByClassName('search-query')[0].value;
  url = '/buscar/' + encodeURI(text) + '/1/';
  window.location = url;
  return false;
}

<form>元素中,您需要添加onsubmit="return dosearch(this)"

这是jsfiddle中的一个例子:
https://jsfiddle.net/dekelb/gkbgqzq4/