.htaccess规则:网址末尾的斜线会弄乱所有网址

时间:2014-05-03 20:23:35

标签: .htaccess url-rewriting

我的.htaccess

中有以下规则
<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 个答案:

答案 0 :(得分:1)

您应避免与*匹配并改为使用+。更重要的是,你的规则是&#34;贪婪&#34;这就是为什么要匹配的原因。您应该将(.*)替换为([^/]+)/只匹配字符串website.com/uleiuri/category-name/brand-name/字符和至少一个字符。

当用户使用您的规则输入地址:cat时,category-name/brand-name变量填充brand<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> 是空字符串,这可能是原因所有产品都归还。

您的规则应如下:

{{1}}