我正在拼命地尝试构建一个类似 API 的 REST。因此,我创建了一个 .htaccess 文件,该文件应根据请求方法将请求重定向到正确的文件。
我设法让它适用于 GET 和 POST 请求,但是当我尝试添加第三个请求方法(如 PUT 或 DELETE)时出现问题。
这是我的 htaccess(短路):
#######################
# GET DATA URIs
#######################
RewriteCond %{REQUEST_METHOD} =GET
##### EMPLOYEE
# Load all employees
RewriteRule ^rest/employee$ /moduls/employee/rest/get/get_all.php
# get all data form a specific employee
RewriteRule ^rest/employee/([0-9]*)$ /moduls/employee/rest/get/get.php?id=$1
# And the employee sub resources
# BASE DATA
RewriteRule ^rest/employee/([0-9]*)/base_data$ /moduls/employee/rest/get/base_data.php?id=$1
# CONTRACT DATA
RewriteRule ^rest/employee/([0-9]*)/contract$ /moduls/employee/rest/get/contract.php?id=$
#######################
# CREATE DATA URIs
#######################
RewriteCond %{REQUEST_METHOD} =POST
# REST URLs for employee
# Add new employee
RewriteRule ^rest/employee$ /moduls/employee/rest/add.php
#######################
# EDIT DATA URIs
#######################
RewriteCond %{REQUEST_METHOD} =PUT
##### EMPLOYEE
RewriteRule ^rest/employee/([0-9]*)/base_data$ /moduls/employee/rest/edit/base_data.php?id=$1
当我开始向它发出请求时rest/employee
,取决于执行脚本的方法。这很好用,但是当我尝试/rest/employee/XXX/base_data
使用 PUT 请求加载时,apache 总是将我重定向到允许 GET 请求时应该执行的脚本。
我对 .htaccess 很陌生,看不到错误在哪里:-/