htaccess子文件夹中的重写规则 [英] htaccess rewrite rule in subfolder

查看:89
本文介绍了htaccess子文件夹中的重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在核心php中创建REST API方法

Hi i'm trying to create a REST API method in core php

我创建了一个控制器类和方法类,以根据引用调用我的函数

I created a controller class and method class to call my function as per the reference

http:// phppot。 com / php / php-restful-web-service /

我在我当前正在创建此文件的服务器的子文件夹中有文件面对访问重写URL的问题,如何解决以下问题是我尝试过的htaccess规则。

I have my files in sub-folder in my server where i'm currently creating this i face issue on accessing rewrite URL , how can u solve this issue below is the htaccess rule which i tried.

RewriteEngine on
RewriteBase /test/rest/
# map neat URL to internal URL
RewriteRule ^/test/rest/mobile/list/$   /test/rest/RestController.php?view=all [nc,qsa]
RewriteRule ^/test/rest/mobile/list/([0-9]+)/$   /test/rest/RestController.php?view=single&id=$1 [nc,qsa]


推荐答案

问题在于模式,因为没有前导斜线,请参见按目录重写

The problem is with the pattern, because there is no leading slash, see Per-directory Rewrites


每个目录的重写


  • ...

  • 已删除的前缀总是以斜杠结尾,这意味着匹配是针对从不带有前导斜杠的字符串进行的。因此,具有^ /的模式在每个目录上下文中都不会匹配。

  • ...
  • The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.

因此,要解决此问题删除模式中的前导斜杠

So to fix this remove the leading slash in your patterns

RewriteRule ^test/rest/mobile/list/$ /test/rest/RestController.php?view=all [NC,QSA]
RewriteRule ^test/rest/mobile/list/([0-9]+)/$ /test/rest/RestController.php?view=single&id=$1 [NC,QSA]






不相关,但您不在这种情况下,不需要 RewriteBase ,因为您已经使用了绝对替换URL。


Unrelated, but you don't need RewriteBase in this case, because you already use absolute substitution URLs.

此外, QSA | qsappend 是仅在需要时(如果您希望请求中包含查询字符串)。

Also, QSA|qsappend is only needed, if you expect that the requests have a query string.

这篇关于htaccess子文件夹中的重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆