2012-11-01 103 views
1

可能重複:
How to: URL re-writing in PHP?乾淨的URL在PHP

我想通過搜索表單乾淨的URL在的index.php(MVC)

即:

<form> 
    <input type="text" name="search" onClick="if(this.value=='Search') this.value=''" onBlur="if(this.value=='') this.value='Search'" value="Search"/> 
    <button>Search</button> 
</form> 

它發送到的index.php並根據搜索參數即$_GET['search']它被傳遞給搜索控制器

但是,相反的index.php?search=xyz我想通過/search/xyz

+0

歡迎#1。請在發佈*任何*問題之前,按照您需要確認的[問題建議](http://stackoverflow.com/questions/ask-advice)進行確認。請記住,只有你想要的東西,你問自己如何編程不符合本身的編程問題。 – hakre

回答

1

你可以在JS做到這一點的形式

<form onsubmit="myFunc();"> 
.. 

的功能的onsubmit:

function myFunc(){ 
    var search_input = document.getElementById("search_input_id"); 
    window.location = "http://www.yoursite.com/search/"+search_input.value;} 

我不知道這會工作,但你的想法。

UPD:你的情況:<button onclick="muFunc();">

而如果你的意思是 「?如何重定向 'http://www.yoursite.com/search/search_term' 的index.php搜索= SEARCH_TERM」 魯本Nagoga是100%正確的

2

如果您使用的是Apache瞭解mod_rewrite你應該添加重寫規則將改寫/search/xyzindex.php?search=xyz類似:

RewriteEngine on 
Options +FollowSymlinks 
RewriteBase/

RewriteRule ^search/(.*) index.php?search=$1 [R]