【问题标题】:RESTful Application .htaccess errorRESTful 应用程序 .htaccess 错误
【发布时间】:2012-11-03 12:44:33
【问题描述】:

我正在开发一个backbone.js 应用程序(仅供参考),我可以向controller.php 提交请求以进行处理。我在 REQUEST_METHOD 上有一个 switch 语句,它可以与 GETPOSTPUTDELETE 一起正常工作。

但是,当我使用以下.htaccess 文件时:

RewriteEngine On
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule ^/?controller/([^/]+)$ /controller.php?id=$1 [L]

...并传递给 controller/5 而不是 controller.php?id=5 我收到 405 错误 - 方法不允许。

我一直在通过 Google 挖掘并询问同事,但找不到解决方案。

【问题讨论】:

    标签: apache .htaccess rest httprequest


    【解决方案1】:

    .htaccess 的最后一行看起来不对。

    如果你只有方法 0 到 9,它会是这样的:

    RewriteRule ^/controller/([0-9])$ /controller.php?id=$1 [NC,QSA,L]
    

    或一般来说:

    RewriteRule ^/controller/(.*)$ /controller.php?id=$1 [NC,QSA,L]
    

    在这种情况下,controller/ 之后的所有内容都将作为 id 参数的值 ($1) 传递。

    如果在您调用 controller.php?id=5 时它可以工作,那么它在调用 controller/5 时也应该可以工作

    【讨论】:

    • 重写器末尾可以有任何内容 - 我以 ID 为例。
    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2018-03-04
    • 2013-06-17
    • 1970-01-01
    • 2015-01-27
    • 2010-09-20
    • 2012-10-16
    相关资源
    最近更新 更多