Home > Net >  APIs Route in Laravel
APIs Route in Laravel

Time:11-15

I have a route like this in api.php:

...
Route::resource("infos",PaymentController::class)->only([
      'show','store'
  ]);
...

And when I call my API like these:

  • /api/infos/ABC123 => success with Status Code: 200 (in access.log)
  • /api/infos/ABC123/ => there area 2 log (Status code: 301; then Status code 200)
  • /api/infos/ABC123//// => there area 2 log (Status code: 301; then Status code 200)

Why when I add slash symbols, there are 2 line in access.log?

Thanks!

CodePudding user response:

Because when you add / then Laravel redirect it to address without /.

CodePudding user response:

The .htaccess file that ships with Laravel has a section in it that strips trailing slashes by using a redirect.

This is from the .htaccess file, which can be found here:

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

So, the first line you're seeing is the redirect triggered by this .htaccess code, and the second line is the final request without the slash.

  • Related