Question

I have a website where at the moment, it is possible to search by simply typing a value into the url after /search/ For example to search for the word hello, i just use url: www.mywebsite.com/search/hello

So i now want to create a very simple search box to search like that, but i do not want to have = ? inserted into my url

For example at the moment i have code

<form action="http://www.mywebsite.com/search/" method="get"><input class="nice_search" type="text" name="q" placeholder="Search...">
 <input class="nice_submit" type="submit" value="SEARCH"></form>

but this is producing urls which look like http://www.mywebsite.com/search?q=hello when i only want http://www.mywebsite.com/search/hello

Was it helpful?

Solution 2

You can use javascript/jquery to do what you want. Simply add the submit event on the form and prevent default. In that event you get the search value and send the user to the new page search/hello. Let me know if you need help gretting started with the code.

Notice the added id="searchForm"

<form id="searchForm" action="http://www.mywebsite.com/search/" method="get"><input class="nice_search" type="text" name="q" placeholder="Search...">
 <input class="nice_submit" type="submit" value="SEARCH"></form>

Something like this

$('#searchForm').submit(function(e){
   e.preventDefault();
   var search = $('.nice_search').val();
   var url = $(this).attr('action');
   //alert('search='+search+ ' url='+url);
   window.location.href = url+search;
});

OTHER TIPS

mod_rewite, is a better approach than client side:

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?q=([^&]+)(&.*)?$ [NC]
RewriteRule ^search/$ /%2? [R=301,L]

alternative if you care to test:

RewriteEngine On
RewriteRule ^search/([^/]*)$ /search?q=$1 [L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top