我希望能够使用以下网址:
http://www.example.com/valOne/valTwo/valThree/valFour/valFive
并将其转换为:
http://www.example.com/index.php?one=valOne&two=valTwo&three=valThree&four=valFour&five=valFive
我真的只需要为我的应用程序提供一些查询参数,因此正则表达式可以有这五个硬编码,但如果它可以动态创建新的查询参数,因为其他“文件夹”被添加到URL,这将是很好的。此外,并非所有五个文件夹或QP始终存在,因此它必须能够正确处理。
答案 0 :(得分:6)
这是符合您需求的mod_rewrite规则:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?$ index.php?one=$1&two=$2&three=$3&four=$4&five=$5
但是如果用PHP解析所请求的URI路径肯定会更容易:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ index.php
$_SERVER['REQUEST_URI_PATH'] = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
$segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));
var_dump($segments);