简体   繁体   English

Lumen POST 到 root 没有尾部斜杠会导致 301 重定向到带有尾部斜杠的 GET

[英]Lumen POST to root without trailing slash causes 301 redirect to GET with trailing slash

I need to use Laravel's Lumen to create some microsservices.我需要使用 Laravel 的 Lumen 来创建一些微服务。 In this scenario, I need to GET and POST to the root route ("/") of my application, to get a list of courses and create a new course, respectively.在这种情况下,我需要 GET 和 POST 到我的应用程序的根路由(“/”),以分别获取课程列表和创建新课程。

My web.php looks like this:我的web.php看起来像这样:

$router->get("/", "ApiModule@list");
$router->get("/{id}", "ApiModule@read");
$router->post("/", "ApiModule@create");
$router->put("/{id}", "ApiModule@update");
$router->delete("/{id}", "ApiModule@delete");

When I make a request (tested with Postman, Insomnia and JS fetch through Chrome's console), the POST / route without a trailing slash , it redirects with 301 status code to GET / with a trailing slash , loosing my original request.当我发出请求(通过 Chrome 的控制台使用 Postman、Insomnia 和 JS fetch 进行测试)时, POST /路由没有尾部斜杠,它使用 301 状态代码重定向到带有斜杠GET / ,失去了我的原始请求。 But, when I make a POST / request with a trailing slash , it works as expected.但是,当我使用尾部斜杠发出POST /请求时,它会按预期工作。 And, this behaviour doesn't happen on GET / requests (even with and without a trailing slash, it works as expected).而且,这种行为不会发生在GET /请求上(即使有和没有斜杠,它也可以按预期工作)。

So, in Lumen, there is something that redirects POST requests to root uri to GET requests, breaking the expected behaviour.因此,在 Lumen 中,有一些东西会将 POST 请求重定向到 root uri 到 GET 请求,从而破坏了预期的行为。

I've tried to modify the /public/.htaccess file, adding a RewriteCond to the "Redirect Trailing Slashes If Not A Folder..." section.我尝试修改/public/.htaccess文件,将 RewriteCond 添加到“如果不是文件夹则重定向尾斜杠...”部分。 My .htaccess file looks like this now:我的 .htaccess 文件现在看起来像这样:

[...]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^ %1 [L,R=301]
[...]

But stills not working.但仍然无法正常工作。

Can someone help me with this?有人可以帮我弄这个吗? I need POST requests to root address to work even with and without a trailing slash in URL.我需要对根地址的 POST 请求,即使在 URL 中有和没有斜杠的情况下也能正常工作。 Other POST requests to any other route than root works fine.除根以外的任何其他路由的其他 POST 请求都可以正常工作。 This problem is faced only with root route ("/").这个问题只遇到根路由(“/”)。 Already tried to declare the route with an empty string instead of "/" (this way: $router->post("", "ApiModule@create") ), but doesn't work too.已经尝试使用空字符串而不是 "/" 声明路由(这种方式: $router->post("", "ApiModule@create") ),但也不起作用。

Thanks!谢谢!

You can do something like this yourLumenFolder/public see below code.您可以执行类似yourLumenFolder/public操作,请参见下面的代码。

Options -MultiViews
RewriteEngine On
RewriteBase /yourLumenFolder/public/

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ $1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM