可以说,我有以下GET变量可用:
lets say i have the following GET variables available:
状态
城市
卧室
浴室
键入
价格
现在我想他们出来是这样的:
now i want them to come out like this:
mysite.com/state/city /# - 卧室/# - 浴室/类型/价格
mysite.com/state/city/#-bedrooms/#-bathrooms/type/price
不过,我想这个工作,这样,如果这些变量之一是不存在,它仍然会正常工作
however, i want this to work so that if one of these variables are not there, it will still work
即:
mysite.com/state/city /# - 卧室
mysite.com/state/city/#-bedrooms
或
mysite.com/state /# - 卧室/价格
mysite.com/state/#-bedrooms/price
我如何做到这一点?
您可以使用?
量词正则表达式的可选配件。
You can make parts of a regex optional using the ?
quantifier.
# state city bedrooms bathrooms
RewriteRule ^(w+)(?:/(w+))?(?:/(d+)-bedrooms)?(?:/(d+)-bathrooms)?$
script.php?state=$1&city=$2&bedrooms=$3&bathrooms=$4
# add further (?:(d+)-placeholders)? for the other optional parts
然而,这将改写空变量,如果一个子模式不匹配。
This will however rewrite to empty variables if a subpattern is not matched.
因此,也许你应该宁可定义列表中的规则集具有不同specificness的的:
So maybe you should rather define a list of RewriteRules with varying specificness:
RewriteRule ^(w+)/(d+)-bedrooms$ scr?state=$1&bed=$2
RewriteRule ^(w+)/(w+)/(d+)-bedrooms$ scr?state=$1&city=$2&bed=$3
...