我正在使用的API服务器目前通过$ _GET变量接受请求,如下所示
http://localhost/request.php?method=get&action=query_users&id=1138ab9bce298fe35553827792794394&time=1319225314&signature=d2325dedc41bd3bb7dc3f7c4fd6f081d95af1000
我需要帮助创建一个可以读取替换'&'的网址的HTaccess规则'/'以及'='和'?'用'/'创建一个干净的网址,如
http://localhost/request/method/get/action/query_users/id/1138ab9bce298fe35553827792794394/time/1319225314/signature/d2325dedc41bd3bb7dc3f7c4fd6f081d95af1000
有没有办法做到这一点,同时仍保持api的get参数完整无缺?
此外,这不是唯一有不同请求的请求。
Stackoverflow始终是专家建议的地方,所以提前感谢
答案 0 :(得分:21)
这往往是我对此类问题的标准答案,但是为什么要在.htaccess
规则中执行此操作时,只需在request.php
中执行此操作,那将是更易于维护。只需在PHP脚本中解析$_SERVER['REQUEST_URI']
的值。
//not really tested, treat as pseudocode
//doesn't remove the base url
$params = array();
$parts = explode('/', $_SERVER['REQUEST_URI']);
//skip through the segments by 2
for($i = 0; $i < count($parts); $i = $i + 2){
//first segment is the param name, second is the value
$params[$parts[$i]] = $parts[$i+1];
}
//and make it work with your exsisting code
$_GET = $params;
有了这个,您应该可以请求:
http://example.com/request.php/method/get/action/query_users/id/1138ab9bce298fe35553827792794394/time/1319225314/signature/d2325dedc41bd3bb7dc3f7c4fd6f081d95af1000
或者使用这个简单的.htaccess
:
RewriteRule ^request/ request.php
请求:
http://example.com/request/method/get/action/query_users/id/1138ab9bce298fe35553827792794394/time/1319225314/signature/d2325dedc41bd3bb7dc3f7c4fd6f081d95af1000