2014-05-03 24 views
0

我有以下規則我.htaccess的.htaccess規則:在網址的結尾削減打亂了所有的URL

<IfModule mod_rewrite.c> 
    RewriteCond %{REQUEST_FILENAME} -f [OR] 
    RewriteCond %{REQUEST_FILENAME} -d 
    RewriteRule .* - [L] 

    # single product page 
    RewriteRule ^uleiuri/(.*)/(.*)/(.*)-([0-9]+)/?$ produs.php?id=$4 [L] 

    # oils page by category and brand 
    RewriteRule ^uleiuri/(.*)/(.*)/?$ uleiuri.php?cat=$1&brand=$2 [L] 

    # oils page by category 
    RewriteRule ^uleiuri/(.*)/?$ uleiuri.php?cat=$1 [L] 
</IfModule> 

它所做的:website.com/uleiuri顯示所有產品,website.com/uleiuri/category-name顯示所有產品某一類,website.com/uleiuri/category-name/brand-name,顯示所有的產品,爲某一類,也有一定的品牌,最後website.com/uleiuri/category-name/brand-name/name-of-the-product-x是產品頁面。

現在它可以工作,但是如果我在其中任何一個末尾添加/,則規則會失敗並顯示所有產品,例如website.com/uleiuri/category-name/brand-name/會返回所有產品。

我希望這個問題是清楚的,我感謝你的幫助。

回答

1

你應該避免與*匹配和使用+代替。更重要的是,你的規則是「貪婪」的,這就是爲什麼要匹配的原因。此時應更換(.*)([^/]+)將只匹配字符串/字符和至少一個字符。

當用戶輸入地址:website.com/uleiuri/category-name/brand-name/使用您的規則,cat可變充滿category-name/brand-namebrand是一個空字符串,這可能是爲什麼所有的產品都返回。

你的規則應該看起來像那些:

<IfModule mod_rewrite.c> 
    RewriteCond %{REQUEST_FILENAME} -f [OR] 
    RewriteCond %{REQUEST_FILENAME} -d 
    RewriteRule .* - [L] 

    # single product page 
    RewriteRule ^uleiuri/([^/]+)/([^/]+)/([^/]+)-([0-9]+)/?$ produs.php?id=$4 [L] 

    # oils page by category and brand 
    RewriteRule ^uleiuri/([^/]+)/([^/]+)/?$ uleiuri.php?cat=$1&brand=$2 [L] 

    # oils page by category 
    RewriteRule ^uleiuri/([^/]+)/?$ uleiuri.php?cat=$1 [L] 
</IfModule> 
+0

謝謝你的迴應和解釋,現在完美的作品。 –