문제

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

도움이 되었습니까?

해결책 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;
});

다른 팁

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]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top