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

birdietorerik's avatar

.htaccess on my server

Hi!

Strugle alot with errors, get error 401 after login Think the problem is in my root .htaccess file

On my localhost, it working fine.

But after install app to server, i get many 401 errors

Here is my .htaccess file on my server

<IfModule mod_rewrite.c>
# That was ONLY to protect you from 500 errors
# if your server did not have mod_rewrite enabled
RewriteEngine On
# RewriteBase /
# NOT needed unless you're using mod_alias to redirect
RewriteCond %{REQUEST_URI} !/public
RewriteRule ^(.*)$ public/ [L]
# Direct all requests to /public folder
</IfModule>


How can i fix this .htaccess file, so my 401 error dosent show

0 likes
22 replies
Sinnbeck's avatar

Just to be sure.. Are you running nginx or apache2 (.htaccess is apache only)

dmcglone27's avatar

This is all that I could get to work for me... I don't have the mod-rewrite.c line like yours does. I don't know if that would make a difference or not, but it's worth a shot.

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/ [L]
birdietorerik's avatar

Hi!

I am using telescope, to show what is going on

I get this error from server:

{
"message": "Unauthenticated."
}

After some checkin of this error message on google I found aout that i must be somthing to do with Authentication

I change Middleware -> Authenticate.php to :

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }

    // Add new method 
    protected function unauthenticated($request, array $guards)
    {
        abort(response()->json([
            'status' => 'false',
            'message' => 'You are not autorized',], 401));
    }
}

When i now run the application, i get message:

{
"status": "false",
"message": "You are not autorized"
}

So the problem is Authentification, but is it .htaccess that are wrong or somthing else ?

Snapey's avatar

You should fix your hosting so that you can use the standard .htaccess

next, check if cookies are being sent to your client (browser tools, application tab in Chrome)

birdietorerik's avatar

@Sinnbeck Hi! Have this .htaccess already for Public folder Need .htaccess for root -> if is that that gives me the errors

Snapey's avatar

No, you need document root to be the public folder. You should not have an htaccess that directs traffic to /public

birdietorerik's avatar

Hi!

Tryed 1000 things here, but stuck :(

Is the errors somthing to do with api token ?

Tray2's avatar

Like @snapey says you should change your document root to /public. However on shared hosting that might be a problem to do.

In that case you can use something like this in your htaccess file (the one in the document root)

#Rewrite everything to subfolder 
RewriteEngine On 
RewriteCond %{REQUEST_URI} !^/public 
Rewriterule ^(.*)$ public/ [L]	
birdietorerik's avatar

Hi all!

This problem is related to server problem ? or is it laravel ?

Found out that error 401 is related to ->AuthServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/dashboard';
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     */
    public function boot()
    {
        parent::boot();
    }

    /**
     * Define the routes for the application.
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }
}

Change function mapApiRoutes to:

protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }

Now, 401 errors is gone, but language dosent work or google maps infowindow. Guess middleware('web') must be middleware('api') ?

So what do i do ????

Please or to participate in this conversation.