Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ozan's avatar
Level 32

Route Redirect Loop Problem

Hey, I am having a redirection loop problem. Please help me out.

Route::group(['prefix' => 'blog', 'namespace' => 'Blog'], function()
{
    Route::get('/', ['as' => 'blog.home', 'uses' => 'PostsController@index']);
    Route::resource('posts', 'PostsController');
    Route::resource('tags', 'TagsController');
    Route::resource('comments', 'CommentsController');
    Route::resource('likes', 'LikesController');
});

I go to this url: ozankurt.dev/blog

And I get redirect loop error from chrome.

Here is my Controller and Repository:

class PostsController extends Controller {

    private $postRepository;

    function __construct(PostRepository $postRepository)
    {
        $this->postRepository = $postRepository;
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        dd($this->postRepository->getAllPaginatedWithTagsAndOwners(5));
        return view('blog.pages.home')
            ->with('posts', $this->postRepository->getAllPaginatedWithTagsAndOwners(5));
    }
}

class DatabasePostRepository implements PostRepository
{

    public function getAllPaginatedWithTagsAndOwners($postsPerPage)
    {
        return Post::with(['owner', 'tags'])->paginate($postsPerPage);
    }

}
0 likes
28 replies
bobbybouwmann's avatar

If you go to your command line and type this you will get a list of all routes, maybe there is a mistake (check for double routes)

php artisan route:list

Just a side note, do you really need resources for all of them? That are a lot of routes that will never be used!

RachidLaasri's avatar

You still get the redirection loop when you

dd($this->postRepository->getAllPaginatedWithTagsAndOwners(5));

or just when using a view?

Ozan's avatar
Level 32

@RachidLaasri No return... Just loop. :/

@blackbird It's just a fun project, I don't really care about that. But still I have the route on route:list everything totally work except that route.

bobbybouwmann's avatar

And you comment out all your routes and uncomment them one by one, then you know which controller is causing the issue :)

Ozan's avatar
Level 32

@blackbird @JeffreyWay I have the same problem with admin page too...

$ php artisan route:list
+--------+----------+-------+------------+-------------------------------------------------+------------+
| Domain | Method   | URI   | Name       | Action                                          | Middleware |
+--------+----------+-------+------------+-------------------------------------------------+------------+
|        | GET|HEAD | admin | admin.home | App\Http\Controllers\Admin\HomeController@index |            |
+--------+----------+-------+------------+-------------------------------------------------+------------+

There is only 1 route...

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function()
{
    Route::get('/', ['as' => 'admin.home', 'uses' => 'HomeController@index']);
});

Result is: There is redirect loop on this site. (Chrome)

bobbybouwmann's avatar

So then the namespace must be the issue. Are you sure the controller has the correct namespace.

What happens when you remove the namespace and give that a try ;)

Ozan's avatar
Level 32

@blackbird

Same Thing :'( Seriously I am going to cry soon!

$ php artisan route:list
+--------+----------+-------+------------+-------------------------------------------+------------+
| Domain | Method   | URI   | Name       | Action                                    | Middleware |
+--------+----------+-------+------------+-------------------------------------------+------------+
|        | GET|HEAD | admin | admin.home | App\Http\Controllers\HomeController@index |            |
+--------+----------+-------+------------+-------------------------------------------+------------+
Route::group(['prefix' => 'admin'], function()
{
    Route::get('/', ['as' => 'admin.home', 'uses' => 'HomeController@index']);
});
bobbybouwmann's avatar

What is the index function doing? It should be redirecting all the time, never had that issue with Laravel

bobbybouwmann's avatar

Does that view exists? It might be redirecting because it has the wrong view and if it redirects to that view you get this issue ;)

Ozan's avatar
Level 32

No issue with view and the blade file because it works when I use it from my root page /

Ozan's avatar
Level 32

@bashy

$ curl -I http://ozankurt.dev/admin
HTTP/1.1 301 Moved Permanently
Date: Sun, 15 Feb 2015 16:42:51 GMT
Server: Apache/2.4.9 (Win64) PHP/5.5.12
Location: http://ozankurt.dev/admin/
Content-Type: text/html; charset=iso-8859-1


1978@1978-PC /C/wamp/www/GITHUB/OzanKurt (ma
$ curl -I http://ozankurt.dev/admin/
HTTP/1.1 301 Moved Permanently
Date: Sun, 15 Feb 2015 16:42:59 GMT
Server: Apache/2.4.9 (Win64) PHP/5.5.12
Location: http://ozankurt.dev/admin
Content-Type: text/html; charset=iso-8859-1
bobbybouwmann's avatar

It redirects to the same page... You must have a redirect somewhere.. (301 Moved Permanently)

bashy's avatar
bashy
Best Answer
Level 65

So it's a slash problem. Probably related to htaccess or Nginx rewrite. Do you have an admin folder?

Related to the 301 redirect in the htaccess for trailing slash (when a directory exists).

Ozan's avatar
Level 32

Yea the slash appears and disappears when trying to load on browser...

I am using wamp.

But my .htaccess is laravel default

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
bashy's avatar

Remove the rewrite for trailing slash and try it. That's broken anyway.

Also put the !-d below !-f seems to fix the checking for directories over files

Ozan's avatar
Level 32

Found the issue!!!

It doesn't allow me to put a folder called admin to public directory...

A url cannot have the same name/ with a directory in public

OMG! Thank you!

bashy's avatar

Well of course not, route /admin and a folder called admin will be the same as each other.

Ozan's avatar
Level 32

@bashy Say that :D I am so dumb :'( I cry! I have been thinking about this problem for days...

Which comment should I mark as "the Answer"?

bashy's avatar

Yeah glad it's solved.

Probably "So it's a slash problem. Probably related to htaccess or Nginx rewrite. Do you have an admin folder?"

Ozan's avatar
Level 32

@bashy You are out of point limits man :D Slow down. :P

Ozan's avatar
Level 32

@bashy please edit that comment and add the thing with

The same route name which is the same with the directory name won't work...

bashy's avatar

People should be able to see it's related to that. :)

Please or to participate in this conversation.