Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: API requests with a trailing forward slash #3330

Closed
djsaturn opened this issue Jul 16, 2020 · 6 comments
Closed

Bug: API requests with a trailing forward slash #3330

djsaturn opened this issue Jul 16, 2020 · 6 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@djsaturn
Copy link

djsaturn commented Jul 16, 2020

I spent a lot of time trying to figure out why my routes were not working and found that I needed to exclude the trailing forward slash.

$routes->post('API/news', 'API\News::create');

$routes->get('API/news', 'API\News::view'); 

Example:
POST http://localhost/API/news/ will send you to the GET (http://localhost/API/news/view) route
POST http://localhost/API/news will work correctly

Even if I set my route with the trailing forward slash I still get routed to API\news::view.

CI Version 4.0.4

@djsaturn djsaturn added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 16, 2020
@bagimuka
Copy link
Contributor

$routes->post('API/news', 'YourController::create');
$routes->get('API/news', 'YourController::view');

@michalsn
Copy link
Member

Not really a bug. Trailing slashes are redirected by default. But now I started to wonder if these rules should be enabled by default.

If you want to change this behavior, edit your .htaccess file and comment out these two lines responsible for removing trailing slashes from request https://github.com/codeigniter4/CodeIgniter4/blob/develop/public/.htaccess#L19

@djsaturn
Copy link
Author

djsaturn commented Jul 18, 2020

I see, not sure what the purpose is of redirecting on trailing slashes but when calling these routes as a resource the results are not correct and can cause some frustration trying to figure out what's wrong. Took me a few hours to figure this one out.

Removing the lines in the .htaccess fixes the issue.

Thanks!

@paulbalandan
Copy link
Member

In this article, Google's John Mueller clarified what counts as duplicate content in this tweet.

I noticed there was some confusion around trailing slashes on URLs, so I hope this helps. tl;dr: slash on root/hostname=doesn't matter; slash elsewhere=does matter (they're different URLs)
image

@djsaturn
Copy link
Author

On all my internal APIs that shouldn't be a problem to cover. My only concern is when I have APIs available to the public. Lets say someone uses one of my API end points on Zapier or whatever. They inadvertently add the trailing slash. The user might get frustrated and think my APIs do not work. Just my two cents. Now that I understand the reasoning behind it, I have no problem excluding trailing slashes.

@michalsn
Copy link
Member

Okay, I guess we're leaving this as it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

4 participants