mod_rewrite for REST API on PHP

后端 未结 1 1974
野趣味
野趣味 2021-02-04 19:03

I want that every call with

http://localhost/api/

(as root folder) for example http://localhost/api/get/users act

相关标签:
1条回答
  • 2021-02-04 19:34

    Your (own) answer should be fine, just keep in mind that it will redirect all incoming URLs to index.php. I.e. if you have static files, for example /js/jquery.js then it will be changed to index.php?handler=/js/jquery.js.

    If you want to avoid problems try something like:

    RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
    RewriteRule ^(.*)$ index.php?handler=$1 [QSA,L]
    

    Two hints:

    Please try to use the RewriteLog directive: it helps you to track down such problems:

    # Trace:
    # (!) file gets big quickly, remove in prod environments:
    RewriteLog "/web/logs/mywebsite.rewrite.log"
    RewriteLogLevel 9
    RewriteEngine On
    

    My favorite tool to check for regexp:

    http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

    0 讨论(0)
提交回复
热议问题