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

kofispaceman's avatar

Prevent Laravel from redirecting to Intended URL upon Login

Hello Guys,

I'm using Laravel 5.2 and I'm facing quite an issue. Below is the details:

  1. The Laravel application's home page is at the root. Anytime I visit the root of the application (http://example.app), I am redirected to the login page (http://example.app/login).

  2. After login, I am redirected to the root of the application (http://example.com).

I know this happens because the intended URL was the root of the application. I am trying to change that behavior. I want the user to select the page he/she wants to see upon login, regardless of his/her intended URL and redirect the user to that page upon login. I set the $redirectTo variable but it only works when the first page I visit is the login route and not the base URL. Is there any solution to this?

0 likes
5 replies
ejdelmonico's avatar

protected $redirectTo = '/your-page'; in your LogInController.

What your asking is something that you would have to write code for. Out of the box with auth, Laravel will redirect to the login page if not authenticated when the user selects any page. Use that basis as an idea of how to do it and then redirect to the intended page.

kofispaceman's avatar

Thanks for your answer but I stated in the question that it doesn't work. I am still redirected to the intended page.

kofispaceman's avatar

Funny enough, Laravel redirects to my intended URL. I see many users complaining about redirect()->intended() but Laravel does this out of the box. I am trying to prevent that feature

ejdelmonico's avatar

You only need to return a redirect to wherever you want the user to go. If you want the user to choose a redirect, you will have to implement some type of intercepter to redirect to the route after login. Use a login form value to add to the redirect url to accomplish allowing the user to choose.

    /**
     * Where to redirect users after authentication.
     *
     * @var string
     */
    public static $afterLoginRedirectTo = '/home';

     /**
     * Where to redirect users after authentication.
     *
     * @return string
     */
    public static function afterLoginRedirect()
    {
        return static::$afterLoginRedirectTo;
    }

    /**
     * Set the path to redirect to after authentication.
     *
     * @return void
     */
    public static function afterLoginRedirectTo($path)
    {
        static::$afterLoginRedirectTo = $path;
    }

Please or to participate in this conversation.