HTaccess rule to configure REST API requests

I have an API server I'm working on, it is currently accepting requests via $ _GET variables, as shown below

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

I need help creating an HTaccess rule that can read URLs that replace '&' with '/', as well as '=' and '?' with '/' to create a clean url like

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

Is there a way to do this while keeping all get parameters intact for the api to work?

In addition, this is not the only request that will perform different requests.

Stackoverflow is always a place for expert advice, so thanks in advance

+6
source share
1 answer

This is usually my standard answer to this question, but why are you trying to do this with .htaccess rules , when you can just do it in request.php and it will be much more supported . 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 this, you can request:

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

Or use this simple .htaccess :

 RewriteRule ^request/ request.php 

And request:

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

Source: https://habr.com/ru/post/899820/


All Articles