简体   繁体   中英

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. 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!

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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