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

jr19's avatar
Level 1

htaccess rewrite to /public

Hello,

We're looking to move our core business systems from Codeigniter to a more modern framework. Laravel is looking to be the favourite so far. However, I'm running into some problems, and I'm struggling to find a solution in the forums.

We want to keep everything to do with one project in one folder. I.e. we want to remove /public from each project's url e.g. should be example.com/system1 where system1 contains everything that is generated when a new laravel project is created.

I'm using the default project atm. I've added the following .htaccess to the laravel project folder (/system1). However, it just returns a NotFoundHttpException exception; whilst example.com/system1/public does what I want, and returns the default page.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

I've seen a lot of suggestions to use symbolic links. This isn't an option for us as there are many (25+) systems and websites that we will be migrating, and we're using git deployment on dev, staging and production servers.

To help, the routes.php file is as standard:

Route::get('/', function () {
    return view('welcome');
});

Your help is much appreciated!

Thanks, James

0 likes
8 replies
Snapey's avatar

This is not recommended practice as you end up with all your app code, config items etc accessible from the outside.

So you are saying that all 25 sites are on the same domain but in different sub folders?

If so, I would have a directory structure like

\var\site1 \var\site2 \var\www\site1 \var\www\site2

in the var\site1 folder you have the Laravel installation with your app

in the var\www\site1 folder have the contents of the site1 public folder. In this folder, change the index.php to bootstrap the correct installation.

no, actually what I would do is have each on a different sub domain, hosted on different servers.

jr19's avatar
Level 1

Thanks for your response Snapey!

As we're rewriting example.com/app to example.com/public/app (etc) there is no way that a user will reach the app code files because it will always be looking in the public folder. I like the concept of keeping the app code outside the web directory, but it just won't work for our deployment process and domain structures.

Most of the sites are under separate vhosts, but on one of our domains we have multiple systems in a subfolder e.g. example.com/sys1, example.com/sys2. I can't have it where to access the system someone needs to go to example.com/sys1/public. Similarly because we are using git deployment and we often create new branches and add new systems to the websites, every git repository needs to be in its own folder and not be spread across 2 directories as you proposed.

bashy's avatar

You can use an alias in Apache to redirect your request to the other folder. Make sure to deny access to that folder directly though.

jr19's avatar
Level 1

Right, I've made a bit of progress by printing the request before.

Here's what I'm getting from going to /public

http://example.local/tests/frameworks/laravel/public/test
http://example.local/tests/frameworks/laravel/public
GET
http://example.local/tests/frameworks/laravel/public/test
test
Segments = Array
(
    [0] => test
)

And this is what I'm getting without /public using the rewrite in post 1

URL = http://example.local/tests/frameworks/laravel/test
Root = http://example.local
Method = GET
full URL = http://example.local/tests/frameworks/laravel/test
Path = tests/frameworks/laravel/test
Segments = Array
(
    [0] => tests
    [1] => frameworks
    [2] => laravel
    [3] => test
)

It seems Laravel determines the website root somewhere, but does anyone know what the best way to override it? I have set the url in my app.php to

'url' => 'http://example.local/tests/frameworks/laravel/',

Your help is much appreciated!

Snapey's avatar

hmmm I think you will have a lot of problems with things like asset folder locations, and all your routes will need /tests/frameworks/laravel/ as a prefix

jr19's avatar
Level 1

Mm, that was my fear! I will keep digging and experimenting and post back here if I find a solution! Thanks for your suggestions though.

Please or to participate in this conversation.