Welcome Guest, Not a member yet? Register   Sign In
Getting HTTP 404 when redirecting but reloading works.
#1

Using CodeIgniter 4.5.3, after doing user identification (you know, creating a session variable and updating the database to register the login) I do a redirection to the index page this way:
PHP Code:
return redirect ()->to (''307); 
It always returns a HTTP 404 error, but reloading the page (i.e. [Ctrl]+[R]) loads it without any error.  Log doesn't show any message.

Accessing the index page directly (by putting the URL) doesn't fail.

Doing other redirections (for example, when the index page detects the user isn't identified it redirects to the login page using same code) works without any problem.

Anybody knows any reason for this behavior?
Reply
#2

An empty string for the URL is probably invalid.
 Try to specify "/" or name for the Route. It needs to be configured with the "as" parameter in Routes.php
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

The most common cause of a 404 error is that the route does not exist.

1. Set development mode. https://codeigniter.com/user_guide/insta...pment-mode and see the error message.
2. Check your routes. https://codeigniter.com/user_guide/incom...ark-routes
Reply
#4

(07-15-2024, 10:56 AM)ozornick Wrote: An empty string for the URL is probably invalid.


It is valid. an empty string is the same as `/`.
Reply
#5

(07-15-2024, 03:26 PM)kenjis Wrote: The most common cause of a 404 error is that the route does not exist.

1. Set development mode. https://codeigniter.com/user_guide/insta...pment-mode and see the error message.
2. Check your routes. https://codeigniter.com/user_guide/incom...ark-routes

It's in development mode yet.  No error message except the HTTP 404 error.

I'm not using Spark, I'm using Apache.  Routes checked;  they're correct as the web works.  It only "fails" when redirecting after log-in, any other way accessing the index (writing the URL directly, using a link or reloading when receiving the 404 error) work as expected.

The redirection is done correctly as the browser shows the correct URL.

The thing that puzzles me is why it loads when I reload.

Maybe I'm not explaining (or understanding) it properly.
Reply
#6

(This post was last modified: 07-16-2024, 02:15 PM by kenjis.)

Spark provides useful commands for development. If you use it, it helps a lot.
Code:
$ php spark routes
CodeIgniter v4.5.3 Command Line Tool - Server Time: 2024-07-16 21:13:09 UTC+00:00

+--------+-------+------+------------------------------+----------------+---------------+
| Method | Route | Name | Handler                      | Before Filters | After Filters |
+--------+-------+------+------------------------------+----------------+---------------+
| GET    | /    | »    | \App\Controllers\Home::index |                |              |
+--------+-------+------+------------------------------+----------------+---------------+

Required Before Filters: forcehttps, pagecache
Required After Filters: pagecache, performance, toolbar


When I see CI4's 404 message in development mode, I see a message like this.
Code:
404
Can't find a route for 'GET: 404'.
Reply
#7

(07-15-2024, 10:46 AM)Ñuño Martínez Wrote:
PHP Code:
return redirect ()->to (''307); 

Why did you specify 307 intentionally?
Reply
#8

(This post was last modified: 08-07-2024, 03:18 AM by Ñuño Martínez.)

(07-16-2024, 02:16 PM)kenjis Wrote:
(07-15-2024, 10:46 AM)Ñuño Martínez Wrote:
PHP Code:
return redirect ()->to (''307); 

Why did you specify 307 intentionally?

Because it is not a permanent redirection.

(07-16-2024, 02:14 PM)kenjis Wrote: Spark provides useful commands for development. If you use it, it helps a lot.
But it is a web application, not a console application.  I need a webserver, hence the use of Apache.
Reply
#9

I meant, "Spark provides useful commands for web app development and checking app configuration on product environments."
Reply
#10

Can't you show the output of `php spark routes` and the exact error message when 404 like the following?

Quote:404
Can't find a route for 'GET: 404'.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB