mod_rewrite的:在URL转换'文件夹'进入查询参数 [英] mod_rewrite: Convert 'Folders' in URL Into Query Parameters
问题描述
我希望能够采取URL,如:
http://www.example.com/valOne/valTwo/valThree/valFour/valFive
和将其转换为:
<$p$p><$c$c>http://www.example.com/index.php?one=valOne&two=valTwo&three=valThree&four=valFour&five=valFive我真正需要的只是我的应用程序的几个查询参数,以便定期防爆pression能有这五个硬codeD,而这将是很好,如果它可以动态创建新的查询参数作为额外的文件夹被添加到该URL。此外,不是所有的五个文件夹或QP的永远是present,因此它必须能够句柄适当
下面是满足您的需求的mod_rewrite规则:
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^([^ /] *)/?([^ /] *)/?([^ /] *)/?([^ /] *)/?([^ /] *)/ $指数。 ?PHP 1 = $ 1安培;二= $ 2和;三= $ 3安培;四= $ 4安培;五= $ 5
但是,如果你解析用PHP请求的URI路径将是当然更容易,而不是:
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则!^指数\\ $ .PHP的index.php$ _ SERVER ['REQUEST_URI_PATH'] = preg_replace('/\\?.*/','',$ _ SERVER ['REQUEST_URI']);
$段=爆炸('/',修剪($ _ SERVER ['REQUEST_URI_PATH'],'/'));
后续代码var_dump($段);
I want to be able to take a URL like:
http://www.example.com/valOne/valTwo/valThree/valFour/valFive
and convert it to:
http://www.example.com/index.php?one=valOne&two=valTwo&three=valThree&four=valFour&five=valFive
I really only need a few query parameters for my application so the Regular Expression could have these five hard coded, but it would be nice if it could dynamically create new query parameters as additional 'folders' are added to the URL. Additionally, not all five folders or QPs will always be present, so it must be able handle that appropriately.
Here’s a mod_rewrite rule that meets your needs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?$ index.php?one=$1&two=$2&three=$3&four=$4&five=$5
But it would be certainly easier if you parse the requested URI path with PHP instead:
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);
这篇关于mod_rewrite的:在URL转换'文件夹'进入查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!