2016-11-03 139 views
0

我有兩個網址做一些查詢,我正在努力按照我想要的方式重寫。URL重寫php查詢字符串

首先

/shop/index.php?category=catslug 

,我想成爲

/shop/category 

/shop/index.php?product=slug 

/shop/category/product 

我有這個目前:

RewriteRule ^shop/([A-Za-z0-9-]+)/?$ shop/index.php?category=$1 [L] 
RewriteRule ^shop/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ shop/index.php?product=$1 [NC,L] 

的問題是,有一條規則被破壞任何東西,與店啓動,這樣的東西像不起作用。我很困惑。這可能嗎?

+0

現在無法研究完整答案,但這就是'RewriteCond'的用途。請參閱:http://httpd.apache.org/docs/2.0/misc/rewriteguide.html 還有http://www.askapache.com/htaccess/modrewrite-tips-tricks/的語法和示例 –

回答

0

請注意,對於,cart部分與第一個RewriteRule[A-Za-z0-9-]+部分相匹配。所以它被改寫爲shop/index.php?category=cart

避免這種情況,希望你有一個相對較少的固定網址的方式,就是你的2個規則之前有一個RewriteRule

RewriteRule ^shop/(cart|this|that|other|thing) - [L] 
RewriteRule ^shop/([A-Za-z0-9-]+)/?$ shop/index.php?category=$1 [L] 
RewriteRule ^shop/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ shop/index.php?product=$1 [NC,L] 

匹配的管道分隔字符串中的一個請求(如cart),-表示不改變請求,[L]表示最後(不要繼續下一個規則)。

+0

謝謝說明。這確實有效,儘管現在嘗試訪問這些頁面時會導致404錯誤。這是否與後面的規則有關,刪除後綴.php擴展名? – Doidgey

+0

Scrap that ...我將規則移到了刪除.php擴展名的規則下,並且完美地工作。 – Doidgey