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

Nyleor's avatar

.htaccess rewriting ./ to ./public in Laravel 10

Hello,

I know you think what I am asking is pretty basic, but I couldn't find any answer.

I want to set my Laravel project in a subdirectory, so I'd like to access it at https://www/2023/laravel-project/ instead of https://www/2023/laravel-project/public/

In previous Laravel versions, I could do a basic .htaccess in the root directory :

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} !^/public/?$
    RewriteRule ^(.*)$ public/ [L]
</IfModule>

And it worked fine. Now, in Laravel 10, this doesn't work anymore. I tried many ways, in vain.

Do you have a solution ? It must be possible with an htaccess, isnt' it ??...

I don't want to use php artisan serve !

Thank you.

0 likes
4 replies
hupp's avatar

@nyleor To access project without public prefix follow the steps :

  • Update your Laravel configuration: Edit the bootstrap/app.php file in your Laravel project and modify the public_path() function call to include the subdirectory name. For example, if your subdirectory name is laravel-project, change the line from:
$app->bind('path.public', function () {
    return __DIR__.'/../public';
});

to

$app->bind('path.public', function () {
    return __DIR__.'/../laravel-project';
});

This tells Laravel to use the laravel-project subdirectory as the public directory instead of the default public directory.

assuming you are using the Apache Server: .htaccess changes

RewriteEngine On
<IfModule mod_rewrite.c>
    RewriteRule ^(.*)$ public/ [L]
</IfModule>
Nyleor's avatar

Thank you for your answer. But in Laravel 10, there is no $app->bind('path.public',...). My bootstrap/app.php looks like this :

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

return $app;
LarryMarzanJr's avatar

For Laravel 10, create 2 files in your root directory, that is .htaccess and server.php

For .htaccess:

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

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^ [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/ 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php
</IfModule>

For server.php

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/public/index.php';

Refference: here

Snapey's avatar

and make sure to expose your .env to the world, then post on reddit how your laravel site got hacked

Please or to participate in this conversation.