简体   繁体   English

htaccess子文件夹中的重写规则

[英]htaccess rewrite rule in subfolder

Hi i'm trying to create a REST API method in core php 嗨,我正在尝试在核心php中创建REST API方法

I created a controller class and method class to call my function as per the reference 我创建了一个控制器类和方法类,以根据引用调用我的函数

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

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. 我的文件位于服务器的子文件夹中,当前正在创建此文件,我将在访问重写URL时遇到此问题,您如何在下面解决此问题,这是我尝试过的htaccess规则。

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 问题出在模式上,因为没有前导斜线,请参见按目录重写

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]

Unrelated, but you don't need RewriteBase in this case, because you already use absolute substitution URLs. 无关,但是在这种情况下您不需要RewriteBase ,因为您已经使用了绝对替换URL。

Also, QSA|qsappend is only needed, if you expect that the requests have a query string. 另外,仅当您期望请求包含查询字符串时,才需要QSA|qsappend

try the below code 试试下面的代码

RewriteEngine on
RewriteBase /test/rest/
RewriteRule /(.*)/ RestController.php?view=$1
RewriteRule /(.*)/(.*)/ RestController.php?view=$1&id=$2

I got solution using Rewriting URL tool 我使用重写URL工具获得了解决方案

http://www.webconfs.com/web-tools/url-rewriting-tool/ http://www.webconfs.com/web-tools/url-rewriting-tool/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM