带有可选参数的智能URL?

时间:2014-04-18 19:53:31

标签: php regex .htaccess url

我目前在.htaccess文件中有这个:

RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)(/(\w+))?/?$ route.php?1=$1&2=$3 [L]

所以我可以myurl.com/foo/bar(/)myurl.com/foo(/)(可选的尾随斜杠,不能丢失)

我对正则表达式不是很熟悉,所以我如何才允许最多约6个参数(myurl.com/one/two/three/four/five/six)

1 个答案:

答案 0 :(得分:0)

RewriteRule ^(\w+)(?:/(\w+))?(?:/(\w+))?(?:/(\w+))?(?:/(\w+))?(?:/(\w+))?/?$ route.php?1=$1&2=$2&3=$3&4=$4&5=$5&6=$6 [L]

您可以查看here


要在参数中允许使用空格:

RewriteRule ^([\w\ ]+)(?:/([\w\ ]+))?(?:/([\w\ ]+))?(?:/([\w\ ]+))?(?:/([\w\ ]+))?(?:/([\w\ ]+))?/?$ route.php?1=$1&2=$2&3=$3&4=$4&5=$5&6=$6 [L]