mod_rewrite:如何重定向HTTP DELETE和PUT [英] mod_rewrite: How to redirect HTTP DELETE and PUT

查看:134
本文介绍了mod_rewrite:如何重定向HTTP DELETE和PUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用mod_rewrite在php中编写一个小的rest api。

Im trying to write a little rest api in php by using mod_rewrite.

我的问题是:
如何处理HTTP DELETE和PUT?
例如,网址为:
/ book / 1234

My question is: How do I handle HTTP DELETE and PUT? For example, the url would be: /book/1234

其中1234是图书的唯一ID。我想将这个id(1234)重定向到book.php,id为参数。我已经知道如何在php脚本中读取PUT和DELETE变量,但是如何在mod_rewrite中设置这个重写规则?

where 1234 is the unique id of a book. I want to "redirect" this id (1234) to book.php with the id as parameter. I already know how to read PUT and DELETE variables in the php script, but how do I set this rewrite rules in mod_rewrite?

任何想法?

编辑:
GET的重写规则如下所示:

The rewriting rule for a GET would look like this:

RewriteRule book/([0-9]+) book.php?id=$1 [QSA] 

如何为PUT和DELETE执行参数转发?据我所知HTTP POST,PUT和DELETE使用HTTP Request Body来传输参数值。所以我想我需要在HTTP请求体中附加参数。但我不知道如何用mod_rewrite做到这一点。

How do I do this "parameter forwarding" for PUT and DELETE? As far as I know HTTP POST, PUT and DELETE use the HTTP Request Body for transmitting parameter values. So I guess I need to append the parameter in the HTTP request body. But I have no idea how to do that with mod_rewrite.

我可以做一些混合DELETE和GET吗?

Can I do some kind of mixing DELETE and GET?

RewriteCond %{REQUEST_METHOD} =DELETE
RewriteRule book/([0-9]+) book.php?id=$1 [QSA] 

然后在book.php中,我会使用$ _GET ['id']来检索图书ID,即使HTTP HEADER说HTTP METHOD是DELETE。它似乎不起作用...

Then in book.php I would user $_GET['id'] to retrieve the book id, even if the HTTP HEADER says that the HTTP METHOD is DELETE. It does not seems to work ...

推荐答案


我可以做某种混合DELETE和GET?

Can I do some kind of mixing DELETE and GET?

是的。您无需担心重写规则中的请求方法或PUT主体。

Yes. You don't need to worry about the request method or the PUT body at all in your rewrite rules.

对于您的示例,这意味着:

For your example this means:

mod_rewrite

RewriteRule book/([0-9]+) book.php?id=$1 [QSA]

HTTP请求

PUT /book/1234
=> PUT /book.php?id=1234

PHP脚本

$id = intval($_GET['id']);
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
    // yes, it is. go on as usual
}

进一步说明:GET参数和PUT之间的区别/ POST / DELETE参数是前者是URL的一部分,后者是请求体的后半部分。 mod_rewrite 更改网址,不会触及正文。

For further clarification: The difference between GET parameters and PUT/POST/DELETE parameters is that the former are part of the URL, the latter part of the request body. mod_rewrite only changes the URL and does not touch the body.

这篇关于mod_rewrite:如何重定向HTTP DELETE和PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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