通过.htaccess和PHP美化API的URL

编程入门 行业动态 更新时间:2024-10-04 13:27:12
本文介绍了通过.htaccess和PHP美化API的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑到我正在请求此URL进行JSON api响应.

Consider that I am requesting this url for a JSON api response.

// url that user will type in the browser address bar example/api/user/0001

我有2个选项: 选项A 重定向将像这样.但是浏览器中的地址将与以前相同.

I have 2 Options: Option A The redirection will be like this. But The address in the browser will be the same like before.

example?get1=api&get2=user&get3=0001

?get1=xxxxxx和&get2=xxxxxx,并且很重要.如果网址中有更多的斜杠(/),则会添加更多的GET请求,例如&get4=xxxxx &get5=xxxxx,依此类推.

The ?get1=xxxxxx and &get2=xxxxxx and is IMPORTANT. if there is more slashes (/) in the url it will add more GET request like this&get4=xxxxx &get5=xxxxx and so on.

然后,我将像这样从$_GET['reques']方法获取确切的信息!

Then I will get the exact information form the $_GET['reques'] method like this!

$var_request = $_GET['get1']; $var_type = $_GET['get2']; $var_id = $_GET['get3'];

选项B 重定向将像这样.但是浏览器中的地址将与以前相同.

Option B The redirection will be like this. But The address in the browser will be the same like before.

example?request=api/user/0001

然后我将这样分解api/user/0001来获得确切的信息

Then I will get the exact information by exploding the api/user/0001 like this

// Geting request by $_GET $request = $_GET['request']; // Exploding by the (/) $api = explode("/", $request); // Getting my desired information $var_request = $api[0]; $var_type = $api[1]; $var_id = $api[2];

记住:URL与以前相同.它看起来不应该像?get=这样.该网址在浏览器网址栏中仍应为example/api/user/0001.

Remember: The URL will be same as before. It should not look like ?get= this. The url should be still example/api/user/0001 in the browser url bar.

推荐答案

我认为选项B 会更容易实现.可能的解决方案是-

I think Option B will be easier to do this. Possible solution is-

RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteRule (.+)$ ?request=$1 [L]

它重定向-example/api/user/0001到example?request=api/user/0001,而无需更改浏览器中网址栏中的地址.

It redirects - example/api/user/0001 to example?request=api/user/0001 Without changing the address in the url bar in you browser.

更多推荐

通过.htaccess和PHP美化API的URL

本文发布于:2023-11-02 06:37:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   htaccess   URL   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!