Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

php - htaccess rewrite rule in subfolder

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/

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...