2010-09-01 52 views
0

我想將這四個獲取變量(sch,val,lmt,ord)合併成一個很好看的url re_write。目前我已經在我的.htaccess文件下面,它工作在所有變量都包括在內,但是,如果我只輸入了前兩個變量(SCH & VAL)它打破:在查詢字符串中獲取變量re_writes

RewriteRule ^search/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ 
      search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L] 

www.domain.com/搜索/城市/倫敦/ 5/asc工作 www.domain.com/search/City/London但這不是

+0

這裏有一個重複。我沒有投票結束,因爲答案可能不是你正在尋找的東西:http://stackoverflow.com/questions/2219098/cleaning-up-nested-mod-rewrite-statements – 2010-09-01 13:02:59

回答

2

爲什麼不只是在其之前添加其他重寫規則。 IE:

RewriteRule ^search/([^/]*)$ 
     search.php?sch=$1 [L] 
RewriteRule ^search/([^/]*)/([^/]*)$ 
     search.php?sch=$1&val=$2 [L] 
RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)$ 
     search.php?sch=$1&val=$2&lmt=$3 [L] 
RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)/([^/]*)$ 
     search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L] 

這應該有效。不知道它是否是你想要的,但我並不認爲這樣做是「可能的」,而不只是做一個查詢並將完整的查詢字符串作爲一行傳給PHP端。

編輯

關於做的這一切在.htaccess與PHP是它需要較少的邏輯和你傳遞你想要什麼到你的腳本中的正確值的好處。我的首選是使用上面的.htaccess。

2

你可以用這個來抓取整個路徑作爲文本和解析 www.domain.com/search/City/London/5/asc工程www.domain.com/search/City/London但是這並不

RewriteRule ^search/(.*)$ search.php?url=$1 
$paths=explode('/',$_GET('url')); 

輸出
www.domain.com/search/City/London/5/asc

$路徑=城市,倫敦,5,ASC

www.domain.com/search /城市/倫敦

$路徑=城市,倫敦

您可以使用循環和對鍵/值...如果需要