PHP上restapi的mod_重写
我要你的每一个电话PHP上restapi的mod_重写,php,api,rest,mod-rewrite,Php,Api,Rest,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中的何处我将捕获$\u GET variablehandler,并以正确
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中的何处我将捕获$\u GET variablehandler,并以正确的方式处理它
如果我有这种重写规则,它只适用于一个
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&handler2=$2&handler3=$3 [QSA,L]
所以对于第一种情况http://localhost/api/a
可以工作,但http://localhost/api/a/b
将导致找不到错误
编辑:这样可以吗
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的工具:
(不要忘记选择ereg(POSIX)而不是preg(PCRE)!+1用于跳过静态文件条件。据我所知,
RewriteLog
和RewriteLogLevel
指令已从Apache>=2.4中删除。尝试类似于LogLevel warn mod_rewrite.c:trace3
的方法。
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On