mod_rewrite RewriteCond and query string

爷,独闯天下 提交于 2019-12-13 07:29:07

问题


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

or something similar.

As you can see parameters are not fixed. Is there anyway to achieve this? Any help would be greatly appreciate.

Thanks


回答1:


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.



来源:https://stackoverflow.com/questions/3173271/mod-rewrite-rewritecond-and-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!