【问题标题】:How to handle trailing slash in nginx?如何处理 nginx 中的斜杠?
【发布时间】:2016-03-04 20:35:24
【问题描述】:

我有一个精简的 API 和一个总是在每次调用结束时添加斜杠的应用程序。 无法更改调用,因此我寻找一种方法来删除所有 api 调用的尾部斜杠。

我该怎么做?

这是我尝试过的:

rewrite ^/api/(.*)/$ /v1/index.php?$args;

【问题讨论】:

  • [/] 有点毫无意义。 [] 定义了一个字符类,单字符类是多余的。所有字符都已经是单字符类。
  • 感谢建议之前尝试的不仅仅是斜线,所以我没有删除括号
  • OK 重定向现在可以正常工作。但最初的问题并没有因此得到解决。还是args有问题?

标签: php nginx slim


【解决方案1】:

您可以为此使用中间件。

use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

$app->add(function (Request $request, Response $response, callable $next) {
    $uri = $request->getUri();
    $path = $uri->getPath();
    if ($path != '/' && substr($path, -1) == '/') {
        // remove trailing slash
        $uri = $uri->withPath(substr($path, 0, -1));
        $request = $request->withUri($uri);
    }

    return $next($request, $response);
});

详情请见http://www.slimframework.com/docs/cookbook/route-patterns.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 2014-05-10
    • 2020-08-03
    • 2012-10-27
    • 1970-01-01
    相关资源
    最近更新 更多