HTaccess 规则整理 REST API 请求 [英] HTaccess Rule To Tidy Up REST API requests

查看:25
本文介绍了HTaccess 规则整理 REST API 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 API 服务器,我正在使用它目前通过 $_GET 变量接受请求,如下所示

I have an API server that I am working on it currently accepts requests through $_GET variables like below

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

我需要帮助创建一个 HTaccess 规则,该规则可以读取替换&"的网址使用/"以及="和?"使用 '/' 创建一个干净的 url,如

I need help creating a HTaccess rule that can read urls that replace the '&' with '/' and also the '=' and '?' with '/' to create a clean url like

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

有什么办法可以做到这一点,同时仍然保持获取参数完整,以便 api 正常工作?

Is there any way of doing this whilst still keeping the get parameters intact for the api to work from?

此外,这不是唯一的请求,还会有不同的请求.

Also this is not the only request there are different requests that will be made.

Stackoverflow 始终是寻求专家建议的地方,所以提前致谢

Stackoverflow is always the place to go for expert advice so thanks in advance

推荐答案

这往往是我对这类问题的标准答案,但是 你为什么要尝试用 .htaccess 规则,当您可以在 request.php 中执行此操作时,更易于维护.只需解析 php 脚本中 $_SERVER['REQUEST_URI'] 的值.

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. Just parse the value of $_SERVER['REQUEST_URI'] in your php script.

//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

或者使用这个简单的.htaccess:

RewriteRule ^request/ request.php

并请求:

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

这篇关于HTaccess 规则整理 REST API 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆