可以说,我已经准备好以下GET变量:有条件的htaccess?
状态
城市
卧室
浴室
型
价格
现在我希望他们能站出来是这样的:
mysite.com/state/city/#-bedrooms/#-bathrooms/type/price
不过,我想这个工作,使如果这些变量中的一个不存在,它仍然会工作
即:
mysite.com/state/city/#-bedrooms
或:
mysite.com/state/#-bedrooms/price
我该怎么做?
可以说,我已经准备好以下GET变量:有条件的htaccess?
状态
城市
卧室
浴室
型
价格
现在我希望他们能站出来是这样的:
mysite.com/state/city/#-bedrooms/#-bathrooms/type/price
不过,我想这个工作,使如果这些变量中的一个不存在,它仍然会工作
即:
mysite.com/state/city/#-bedrooms
或:
mysite.com/state/#-bedrooms/price
我该怎么做?
可以使用?
量词使正则表达式的某些部分可选。
# 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
但是,如果子模式不匹配,这将重写为空变量。
因此,也许你应该比较确定的RewriteRules的列表有不同specificness:
RewriteRule ^(\w+)/(\d+)-bedrooms$ scr?state=$1&bed=$2
RewriteRule ^(\w+)/(\w+)/(\d+)-bedrooms$ scr?state=$1&city=$2&bed=$3
...
这是很容易做到的权利在你的代码:
#.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ /index.php [QSA,L]
你可以用任何逻辑你想处理请求的URI:
#index.php
<?php
$request = explode('/', ltrim($_SERVER['REQUEST_URI'], '/'));
$state = array_shift($request);
$city = array_shift($request);
// and so on
你可以使用实际GET变量。 'mysite.com?state=XX&city=YYY&...' – 2011-06-13 03:51:58
是国家强制还是甚至是可选的?在这种情况下,URI是什么? – anubhava 2011-06-13 03:53:31
这应该可能是PHP处理的事情(不是.htaccess)。 – Frankie 2011-06-13 04:12:59