管腔柱扎根而不落后斜线会导致301重定向拖延斜线

发布于 2025-02-05 14:35:17 字数 1294 浏览 1 评论 0原文

我需要使用Laravel的管腔来创建一些微型服务。在这种情况下,我需要获取并发布到应用程序的根路由(“/”),以获取课程列表并创建新课程。

我的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");

当我提出请求时(与Chrome的控制台一起进行邮递员,Insomnia和JS获取),POST/ Route 拖延斜线,它将301状态代码重定向到get/ ,带有尾巴斜线,失去了我的原始请求。但是,当我在尾随的斜线的情况下进行post/ request 时,它可以按预期工作。而且,这种行为不会在get /< /code>请求上发生(即使有或不带有落后斜线,它也可以按预期工作)。

因此,在管腔中,有一些将邮政请求重定向到root uri以获取请求,从而破坏了预期的行为。

我尝试修改/public/.htaccess文件,在“如果不是文件夹...”部分中添加重新定位。我的.htaccess文件现在看起来像这样:

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

但是静止不起。

有人可以帮我吗?我需要邮政请求才能root地址即使在URL中有或不带有拖延的斜线。除根以外的其他途径还可以正常工作。此问题仅在根路由下面临(“/”)。已经尝试使用空字符串而不是“/”(这样:$ router-&gt; post(“”,“”,“ apimodule@create”)),但已尝试声明该路线,但也行不通。

谢谢!

I need to use Laravel's Lumen to create some microsservices. 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.

My web.php looks like this:

$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. But, when I make a POST / request with a trailing slash, it works as expected. And, this behaviour doesn't happen on GET / requests (even with and without a trailing slash, it works as expected).

So, in Lumen, there is something that redirects POST requests to root uri to GET requests, breaking the expected behaviour.

I've tried to modify the /public/.htaccess file, adding a RewriteCond to the "Redirect Trailing Slashes If Not A Folder..." section. My .htaccess file looks like this now:

[...]
# 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. Other POST requests to any other route than root works fine. 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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

jJeQQOZ5 2025-02-12 14:35:20

您可以执行此类操作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]

You can do something like this yourLumenFolder/public see below code.

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文