我正在尝试设置一些简单的重写规则,但我输入的任何网址都会每次都进入第一个。
RewriteRule ^categories/(.*/)?([0-9]+)?/$ categories.php?category_id=$2 [QSA,NC,L]
RewriteRule ^categories/(.*/)?([0-9]+)?/(.*/)?([0-9]+)?/$ sub-categories.php?category_id=$3 [QSA,NC,L]
RewriteRule ^categories/(.*/)?([0-9]+)?/(.*/)?([0-9]+)?/(.*/)?([0-9]+)?/$ product.php?product_id=$4 [QSA,NC,L]
我基本上想要:
domain.co.uk/categories/category-name/1/转到categories.php domain.co.uk/categories/category-name/sub-category-name/1/转到子类别.php domain.co.uk/categories/category-name/sub-category-name/product-name/1/转到products.php
每个网址末尾的1将是该类别或产品的ID。
任何帮助将不胜感激。
答案 0 :(得分:1)
您在规则中将所有捕获的组视为可选组。这样做:
RewriteRule ^categories/[\w-]+/(\d+)/?$ categories.php?category_id=$1 [QSA,NC,L]
RewriteRule ^categories/[\w-]+/[\w-]+/(\d+)/?$ sub-categories.php?category_id=$1 [QSA,NC,L]
RewriteRule ^categories/[\w-]+/[\w-]+/[\w-]+/(\d+)/?$ product.php?product_id=$1 [QSA,NC,L]