我正在尝试创建一个小型api,我在该文件夹中有一个名为api的文件夹,index.php
而.htaccess
我要尝试执行的操作是在访问api/something
时将最后一个参数转换为,api/?x=something
并在php中检查函数是否something
存在(如果没有显示404则调用它) 。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?x=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
如果访问api
文件夹,它的工作原理,但如果我api/something
没有添加。
如果很重要:文件夹的结构是这样的:root_website_folder/sub_folder/api
当它重写“某物”时,如果存在,x=something
我x
将调用func名称
public function init(){
$func = strtolower(trim(str_replace("/", "", $_REQUEST['x'])));
var_dump($func);
if((int)method_exists($this,$func) > 0){
$this->$func();
}else{
$this->response('', 401);
}
}
您尚未专门为api添加规则。以下应该工作:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_URI} !^/api
RewriteRule ^(.*)$ index.php?x=$1 [QSA,NC,L]
RewriteRule ^api/(.*)$ api/index.php?x=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
这可以通过排除/ api请求使其不被^(.*)$
规则捕获来实现。
通常,您可以在http://htaccess.mwl.be/上测试重写规则(与该规则无关,我觉得它很有用)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句