I have a search form, and I want to change the url from:

I know how to use htaccess, but I don't know how to send user to friendly URL after he submit the form.

Here is my form:

<form action="search" method="get">
    <input type="text" name="search" id="Searchx" placeholder="Search Marketplace" />

lets say that you have a #searchBtn div or span or what ever that triggers the search

            var value = $("input[name='search']").val();
            if (value.trim() != "")
                var searchField = value;
                searchField = searchField.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, ' ');
                searchField = searchField.replace(/ +(?= )/g, "");
                searchField = searchField.replace(/ /g, "+");
                if (searchField != "")
                    window.location.href = "https://www.example.com/search/" + searchField;

I haven't tried it , and of course change the example.com part (it would be better if JS knew the root url , but you can do it this way also)

Thanks! Is there a way to do it with PHP?

I have found a solution using PHP ...

  1. Redirect the form to a php page as a post method.
  2. Use header location to redirect again to the search page.

Easy and simple ...


$search = urlencode($_POST['search']);

header('location: ../search/' . $search);


<form action="php/search.php" method="post">
    <input type="text" name="search" id="Searchx" placeholder="Search Marketplace" />