【问题标题】:.htaccess configuration for Slim PHP REST API on a subdomain?子域上 Slim PHP REST API 的 .htaccess 配置?
【发布时间】:2012-07-01 12:17:04
【问题描述】:

我目前有一个使用Slim Framework 构建的api.php 文件。我想使用 api.mydomain.com(或 mydomain.com/api)之类的东西来引用 API 文件并使用 http://api.mydomain.com 作为基本 url 进行调用。 Slim 文档要求将以下内容添加到您的 .htacess 文件中:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

但如果我是正确的,如果请求的文件不存在,则返回 index.php 文件。如何最好地设置从 api.mydomain.com 或 mydomain.com/api 到 index.php 的重写?我们发现的所有内容都指向使用 .htaccess 进行配置。

我们使用了以下重写规则,但这导致我们的所有 URL 都附加了斜杠 ('/'),​​这也需要我们在 Slim 中重新配置我们的路由。 (例如:“/messages”变成“/messages/”,“/threads?id=123”变成“/threads/?id=123”)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^api\.mydomain\.com [NC]
RewriteRule ^ api.php [QSA,L]

【问题讨论】:

    标签: php api .htaccess slim


    【解决方案1】:

    虽然可以使用一些复杂的 apache 规则来解决这个问题(请参阅包含的链接),但这实际上是 Slim 版本 2.2+ 中的地址,因此您最好只更新您的 slim 版本(phar 版本设置为“2 .”或“2.2.”)。

    http://www.slimframework.com/install

    如果您决定走 apache 路线(不推荐),请查看以下链接:

    https://webmasters.stackexchange.com/questions/23382/how-to-remove-trailing-slashes-from-url-with-htaccess

    htaccess remove .php, .html and trailing slash

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多