【问题标题】:mod_rewrite for PHP RESTful Server用于 PHP RESTful 服务器的 mod_rewrite
【发布时间】:2011-01-05 14:17:38
【问题描述】:

我正在我的 Mac 上为 API 本地开发一个 PHP RESTful 服务器。

我已成功启用 mod_rewrite 并允许站点目录 (~/Sites/api) 覆盖。

在 ~/Sites/api 中是 .htaccess 文件和 index.php。我想将所有对http://localhost/~myusername/api/* 的请求重写为index.php。我需要保留查询参数,仅此而已。

我在 .htaccess 文件中尝试了以下内容:

Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) index.php [QSA,NC,L]

这会产生 500:Internal Server Error。

注释掉 FollowSymLinks 行会产生 403:Forbidden 错误。

我可以很好地访问 index.php 而无需重写。

您能提供的任何帮助将不胜感激。我现在想哭。

谢谢, 罗斯

【问题讨论】:

    标签: apache rest mod-rewrite url-rewriting


    【解决方案1】:
    RewriteRule ^api/(.*)$ index.php?handler=$1 [L,QSA]
    

    处理程序是传递给您的 index.php 脚本的内容。例如,对

    的请求

    http://localhost/~myusername/api/getUser/myusername

    将被重写为

    http://localhost/~myusername/api/index.php?handler=getUser/myusername

    【讨论】:

    • 你应该排除index.php
    • @Gumbo:如果您不介意,请在声明中提供解释。:)
    • @kaziTanvirAhsan 该评论已有三年多的历史了。我不知道它应该是什么意思。
    • @kaziTanvirAhsan 好的,现在我知道它应该是什么意思了:^api/(.*)$ 也将匹配 api/index.php。因此,index.php 应该被排除在此规则之外,例如。 g.,加上一个额外的RewriteCond $1 !=index.php
    【解决方案2】:

    问题是[L] 意味着停止重写这个 URL,但是你正在生成一个带有index.php 的新URL,它也将被重写,导致无限循环。

    添加 RewriteCond 以排除 index.php 被重写。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      相关资源
      最近更新 更多