I have a search form, and I want to change the url from:
https://www.example.com/search?search=term+term
to:
https://www.example.com/search/term+term

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" />
</form>

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

        $("#searchBtn").click(function()
        {
            var value = $("input[name='search']").val();
            if (value.trim() != "")
            {
                var searchField = value;
                searchField = searchField.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, ' ');
                searchField = searchField.replace(/ +(?= )/g, "");
                searchField = searchField.replace(/ /g, "+");
                searchField.trim("+");
                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 ...

<?php

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

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

?>

<form action="php/search.php" method="post">
    <input type="text" name="search" id="Searchx" placeholder="Search Marketplace" />
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.