mac*_*osh 0 regex apache mod-rewrite
我希望能够获取如下的URL:
http://www.example.com/valOne/valTwo/valThree/valFour/valFive
Run Code Online (Sandbox Code Playgroud)
并将其转换为:
http://www.example.com/index.php?one=valOne&two=valTwo&three=valThree&four=valFour&five=valFive
Run Code Online (Sandbox Code Playgroud)
我真的只需要为我的应用程序提供一些查询参数,因此正则表达式可以将这五个硬编码,但如果它可以动态创建新的查询参数,因为其他"文件夹"被添加到URL,这将是很好的.此外,并非所有五个文件夹或QP始终存在,因此它必须能够正确处理.
这是一个满足您需求的mod_rewrite规则:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?$ index.php?one=$1&two=$2&three=$3&four=$4&five=$5
Run Code Online (Sandbox Code Playgroud)
但是如果用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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2856 次 |
| 最近记录: |