php - 使用 PHP 在 Apache 2.4 上配置 RESTful VirtualHost

标签 php apache rest http mod-rewrite

我正在尝试在我的 Apache 2.4 服务器(在 Ubuntu 上)的 VirtualHost 上创建一个 RESTful API。我有一个名为 dbManager.php 的 php 文件,我使用 RewriteRule 使其看起来像一个 api 目录。除了返回 403 错误的 PUT 和 DELETE 命令外,它工作得很好。这是我的 conf 文件的编辑版本:

<VirtualHost *>
  ServerAdmin onigame@gmail.com
  ServerName servername.com
  ServerAlias *.servername.com

  DirectoryIndex index.html index.php
  DocumentRoot /path/to/local/dir/

  <Directory />
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
    <Limit PUT DELETE>
      Require all granted
    </Limit>
  </Directory>

  # RESTful services provided for the fake "api" directory
  RewriteEngine on
  RewriteRule ^/api/(.*)$ /dbManager.php/$1 [L]

  ServerSignature On

  AddDefaultCharset utf-8
</VirtualHost>

好吧,PUT 和 DELETE 仍然没有工作并返回 403。我也担心我真的不想在目录的任何地方允许 PUT 和 DELETE,但只能通过虚拟 api 目录。执行此操作的正确方法是什么?

最佳答案

我已经设法解决了我的问题,但我真的不明白为什么它有效:

<VirtualHost *>
  ServerAdmin onigame@gmail.com
  ServerName servername.com
  ServerAlias *.servername.com

  DirectoryIndex index.html index.php
  DocumentRoot /path/to/local/dir/

  <Directory />
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
  </Directory>

  <Directory /path/to/local/dir/>
    Require all granted
    Satisfy All
  </Directory>

  # RESTful services provided for the fake "api" directory
  RewriteEngine on
  RewriteRule ^/api/(.*)$ /dbManager.php/$1 [L]

  ServerSignature On

  AddDefaultCharset utf-8
</VirtualHost>

我能弄清楚的是,获得 403 意味着它的访问被阻止,而不是 HTTP 请求的类型(这将导致 405,而不是 403)。而访问问题是在本地目录上,因此需要一个特殊的部分。但我真的不明白为什么我放在那里的两行能让事情奏效。 Require 指令,这是有道理的。但是 Satisfy 指令,据我从 documentation 中可以看出, 应默认为 All

然而,当我删除任何一行时,它都不起作用。

关于php - 使用 PHP 在 Apache 2.4 上配置 RESTful VirtualHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29331339/

相关文章:

java - 通过javascript将自动生成的数字插入数据库表

php - 在循环中,某些查询成功,其他查询失败 'No database selected'

java - Apache HTTPClient 抛出 java.net.SocketException : Connection reset for many domains

javascript - 目录 "javascript"被完全忽略

java - 3 x "Syntax error on token(s)"- 基本 Java 声明

java - 如何使用 JAVA 或 C# 中的 Web 服务或 REST API 在 Microsoft Dynamics CRM 2016(版本 8)中创建案例?

带流的 WCF Rest Web 服务

php - 如何在 PHP 中实现 URL 路由

javascript - 无限滚动显示重复结果

c# - 开始使用 QPID