加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP上的REST API的mod_rewrite

发布时间:2020-05-25 09:23:15 所属栏目:PHP 来源:互联网
导读:我想要每次通话 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/

我想要每次通话

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变量处理程序并处理它合适的方式.

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

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&handler3=$3 [QSA,L]

所以对于第一种情况,http:// localhost / api / a会起作用,但http:// localhost / api / a / b会导致Not Found错误.

编辑:这应该没事吗?

RewriteRule ^(.*)$index.php?handler=$1 [L,QSA]
您(自己)的答案应该没问题,请记住它会将所有传入的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

我最喜欢检查regexp的工具:

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

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读