PHP 上 REST API 的 mod_rewrite

发布于 2024-12-23 10:11:26 字数 1229 浏览 2 评论 0原文

我希望每次调用

http://localhost/api/

(作为根文件夹),例如 http://localhost/api/get/users 实际上是 http://localhost/api /index.php?handler=get/users

http://localhost/api/get/user/87 应该是 http://localhost/api/index.php?handler=get/user/87 其中index.php 我会捕获 $_GET 变量 handler 并以正确的方式处理它。

如果我有这种重写规则,它仅适用于一

RewriteRule ^([^/\.]+)/?$ index.php?handler=$1 [QSA,L]

二三

RewriteRule ^([^/\.]+)/?$ index.php?handler=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?handler=$1&handler2=$2 [QSA,L]

斜杠等等...

RewriteRule ^([^/\.]+)/?$ index.php?handler=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?handler=$1&handler2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?handler=$1&handler2=$2&handler3=$3 [QSA,L]

因此,对于第一种情况 http://localhost/api/a 可以工作,但是 http://localhost/api/a/b 会导致 Not Found 错误。

编辑:这样应该可以吗?

RewriteRule ^(.*)$ index.php?handler=$1 [L,QSA]

I want that every call with

http://localhost/api/

(as root folder) for example http://localhost/api/get/users actually is http://localhost/api/index.php?handler=get/users.

http://localhost/api/get/user/87 should be http://localhost/api/index.php?handler=get/user/87 where in index.php I would catch $_GET variable handler and handle it in proper way.

If I have this kind of rewrite rules it works only for one

RewriteRule ^([^/\.]+)/?$ index.php?handler=$1 [QSA,L]

two

RewriteRule ^([^/\.]+)/?$ index.php?handler=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?handler=$1&handler2=$2 [QSA,L]

three slashes and so on...

RewriteRule ^([^/\.]+)/?$ index.php?handler=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?handler=$1&handler2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?handler=$1&handler2=$2&handler3=$3 [QSA,L]

So for the first case http://localhost/api/a would work, but http://localhost/api/a/b would cause a Not Found error.

EDIT: Should this be fine?

RewriteRule ^(.*)$ index.php?handler=$1 [L,QSA]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2024-12-30 10:11:26

您(自己的)答案应该没问题,只需记住它会将所有传入 URL 重定向到 index.php。即,如果您有静态文件,例如 /js/jquery.js 那么它将更改为 index.php?handler=/js/jquery.js

如果您想避免问题,请尝试以下操作:

RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php?handler=$1 [QSA,L]

两个提示:

请尝试使用 RewriteLog 指令:它可以帮助您追踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

我最喜欢的检查正则表达式的工具:

http://www.quanetic.com/Regex(不要忘记选择 ereg(POSIX) 而不是 preg(PCRE)!)

Your (own) answer should be fine, just keep in mind that it will redirect all incoming URLs to index.php. I.e. if you have static files, for example /js/jquery.js then it will be changed to index.php?handler=/js/jquery.js.

If you want to avoid problems try something like:

RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php?handler=$1 [QSA,L]

Two hints:

Please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文