sanjaysamant's avatar

Shared hosting redirect to public always

Options -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

above is my public directory .htaccess file.

========================================================== below is my root directory .htaccess file

RewriteEngine on

RewriteCond %{REQUEST_URI} !^public

RewriteRule ^(.*)$ public/$1 [L]

but still, it redirects to public/dashboard

below is my login method

public function login(Request $request)
{
    $this->validateLogin($request);
    try{
        if (Auth::attempt(['email' => request('email'), 'password' => request('password'), 'status' => 'active', 'user_type' => '...'])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
        elseif (Auth::attempt(['email' => request('email'), 'password' => request('password'), 'status' => 'active', 'user_type' => '...'])) {
            // Authentication passed...
            return redirect()->intended('/dashboard/blog');
        }
        elseif (Auth::attempt(['email' => request('email'), 'password' => request('password'), 'status' => 'active', 'user_type' => '...'])) {
            // Authentication passed...
            return redirect()->intended('/admin');
        }
        else{

            return redirect()->back()->withErrors('Unauthorized user');
        }
    }catch(\Exception $e){

        return redirect()->back()->withErrors($e->getMessage());
    }
}

other login works fine but when the first condition checked it redirects to public/dashboard

0 likes
3 replies
technoigniters's avatar

No one replied or liked this question on Laracasts.com for last 7 years that's why I do not like this platform.

JussiMannisto's avatar

Because it's poorly written, unformatted and doesn't even contain a question. If you make an effort to be understood, you're more likely to get replies.

imranbru's avatar

This happens because Laravel's URL generator detects the /public directory since your Document Root is pointing to the project root instead of the public folder.

To fix the redirection issue, update your app/Providers/AppServiceProvider.php to force the base URL from your config:

public function boot() { if (config('app.env') !== 'local') { \URL::forceRootUrl(config('app.url')); } }

Then, ensure your .env is set correctly:

APP_URL=https://yourdomain.com

Finally, clear your config cache:

php artisan config:clear

This will stop Laravel from prepending /public to your intended() redirects

Please or to participate in this conversation.