我是Mod Rewrite的新手,我刚刚学到了一点,所以我试着为每个get参数编写不同的规则。这是我的完整网址:
localhost/index.php?view=some-page&city=some-city&ward=some-ward&price=some-price&page=some-page
如您所见,有5个获取参数,最后四个(城市,病房,价格,页面)是可选的。所以我为每个可选参数编写不同的规则,如下所示:
RewriteEngine On
RewriteBase /
# No 4 optional parameter
RewriteRule ^([^/]*)/?$ index.php?view=$1 [QSA,NC,L]
# for set city parameter
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?view=$1&city=$2 [NC,L]
# for set price parameter
RewriteRule ^([^/]*)/price/([0-9]+)\.html$ /index.php?view=$1&price=$2 [NC,L]
#for set page parameter
RewriteRule ^([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&page=$2 [NC,L]
# for set city and page parameter
RewriteRule ^([^/]*)/city/([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&city=$2&page=$3 [NC,L]
# for set city, ward and page
RewriteRule ^([^/]*)/city/([^/]*)/ward/([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&city=$2&ward=$3&page=$4 [NC,L]
# all city, ward, price and page are set.
RewriteRule ^([^/]*)/city/([^/]*)/ward/([^/]*)/price/([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&city=$2&ward=$3&price=$4&page=$5 [NC,L]
这看起来很头疼,我不知道我所做的是否有效。所以我在这里问的是:不是为每个案例写不同的规则,我们可以像这样写一行Rewriterule:
RewriteRule ^([^/]*)/city/([^/]*)/ward/([^/]*)/price/([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&city=$2&ward=$3&price=$4&page=$5 [NC,L]
如果没有设置参数城市(空),告诉规则,那么映射网址应该没有像这样的城市:
/index.php?view=$1&&ward=$2&price=$3&page=$4
非常感谢提前。