codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: Redirect in .htaccess file #7446

Closed iRedds closed 1 year ago

iRedds commented 1 year ago

PHP Version

7.4

CodeIgniter4 Version

latest

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

No response

What happened?

If the request matches the conditions of the redirect, then the response code 301 is used. If the request was made using a non-GET method, then the redirect will use the GET method to access the resource, which breaks the application.

Steps to Reproduce

// trailing slash
$routes->get('form', fn () => '<form action="/handler/" method="post"><input type="submit"></form>');

$routes->post('handler', fn () => 'passed');

Because of the trailing slash in the URL, Apache will redirect, but using the GET method, which will cause a 404 error: Can't find a route for 'get: handler'.

Expected Output

Correct redirect.

Anything else?

If you replace the response code with 308 (similar to 301, that is, permanently), then the redirect will be made using the HTTP method of the original request.

kenjis commented 1 year ago

The form tag should be <form action="/handler" method="post">.

iRedds commented 1 year ago

This is just an example to reproduce. Otherwise, in the documentation, you can write "do not use a trailing slash in the URL" and remove the redirect rule in .htaccess

kenjis commented 1 year ago

I believe the rewrite rule is for cases where the end user has added a trailing slash to the URL.