简体   繁体   English

HTaccess规则整理REST API请求

[英]HTaccess Rule To Tidy Up REST API requests

I have an API server that I am working on it currently accepts requests through $_GET variables like below 我有一个我正在处理的API服务器,它目前通过$ _GET变量接受请求,如下所示

http://localhost/request.php?method=get&action=query_users&id=1138ab9bce298fe35553827792794394&time=1319225314&signature=d2325dedc41bd3bb7dc3f7c4fd6f081d95af1000

I need help creating a HTaccess rule that can read urls that replace the '&' with '/' and also the '=' and '?' 我需要帮助创建一个HTaccess规则,该规则可以读取将'&'替换为'/'以及'='和'?'的网址 with '/' to create a clean url like 用'/'创建一个干净的网址

http://localhost/request/method/get/action/query_users/id/1138ab9bce298fe35553827792794394/time/1319225314/signature/d2325dedc41bd3bb7dc3f7c4fd6f081d95af1000

Is there any way of doing this whilst still keeping the get parameters intact for the api to work from? 有没有办法做到这一点,同时仍然保持获取参数完整的api工作?

Also this is not the only request there are different requests that will be made. 此外,这不是唯一有要求提出不同请求的请求。

Stackoverflow is always the place to go for expert advice so thanks in advance Stackoverflow始终是专家建议的地方,所以提前感谢

This tends to be my standard answer to this kind of question, but why are you trying to do this with .htaccess rules when you could just do it in request.php , and that would be much more maintainable . 这往往是我对这类问题的标准答案,但是你为什么要尝试使用.htaccess规则来执行此操作,当你可以在request.php 执行此操作时,这样可以更加可维护 Just parse the value of $_SERVER['REQUEST_URI'] in your php script. 只需在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;

With that in place, you should be able to request: 有了这个,你应该可以要求:

http://example.com/request.php/method/get/action/query_users/id/1138ab9bce298fe35553827792794394/time/1319225314/signature/d2325dedc41bd3bb7dc3f7c4fd6f081d95af1000

Or use this simple .htaccess : 或者使用这个简单的.htaccess

RewriteRule ^request/ request.php

And request: 并要求:

http://example.com/request/method/get/action/query_users/id/1138ab9bce298fe35553827792794394/time/1319225314/signature/d2325dedc41bd3bb7dc3f7c4fd6f081d95af1000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM