mod_rewrite RewriteCond和查询字符串 [英] mod_rewrite RewriteCond and query string
问题描述
最近,我一直在为客户开发一个小项目.我正在考虑使用wordpress样式的漂亮URL格式.我在Google上搜索了很多,并遇到了Apache的mod_rewrite和RewriteRule
,RewriteCond
.但是没有找到任何可以处理随机参数的示例.例如:
recently, I have been working on a small project for a client. I was thinking about using a wordpress style pretty URL format. I googled around a lot and came accross Apache’s mod_rewrite and RewriteRule
, RewriteCond
. But didn't find any example which can handle random of parameters. For example:
-
/index.php?param=1
→/index/param/1
-
/index.php?foo=bar&hour=1&minutes=12
→/index/foo/bar/hour/1/minutes/12
-
/index.php?page=login&attempt=2
→/index/page/login/attempt/2
/index.php?param=1
→/index/param/1
/index.php?foo=bar&hour=1&minutes=12
→/index/foo/bar/hour/1/minutes/12
/index.php?page=login&attempt=2
→/index/page/login/attempt/2
或类似的东西.
如您所见,参数不是固定的.反正有实现这个目标的方法吗?任何帮助将不胜感激.
As you can see parameters are not fixed. Is there anyway to achieve this? Any help would be greatly appreciate.
谢谢
推荐答案
大多数CMS使用PHP做到这一点.
Most CMS does this with PHP.
.htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
PHP
$URI = substr($_SERVER['REQUEST_URI'], 1);
$URIparts = explode("/", $URI);
然后,您获得了网址的各个部分的数组,您可以根据需要对其进行处理.
Then you've got an array of the parts of the url, and you can process them as you want.
这篇关于mod_rewrite RewriteCond和查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!