url rewrite/formatting and php search form

Question

Happy new year!, i have a search form in php, and im using mod_rewrite, now, the thing that i want is this:

My normal url: http://mywebsite.com/buscar.php?p=my+search+string&pag=1

TO:

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

How i can do that?

.htaccess

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

HTML Form

<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>


Show source
| url   | .htaccess   | url-rewriting   | mod-rewrite   | search   2017-01-02 02:01 1 Answers

Answers ( 1 )

  1. 2017-01-02 02:01

    You will have to use javascript for that.

    You can use this function:

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

    And inside your <form> element you need to add onsubmit="return dosearch(this)".

    Here is an example in jsfiddle:
    https://jsfiddle.net/dekelb/gkbgqzq4/

◀ Go back