PHP   发布时间:2019-11-21  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP上的REST API的mod_rewrite大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要每次通话
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)!)

大佬总结

以上是大佬教程为你收集整理的PHP上的REST API的mod_rewrite全部内容,希望文章能够帮你解决PHP上的REST API的mod_rewrite所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。