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

MeiR_ct's avatar

Respond real path of file if a route fails

I plan to install Laravel 5 on my shared hosting, and to set my ".htaccess" to serve "laravel/public" directory when trying to get "/". The problem is I already have some existing sites and other files in my "public_html" directory, which I want them to remain accessible.

My question is how can I handle all the requests to my domain correctly, so the ones that fail to get trapped through Laravel's routes.php and the controllers, will redirect to the real path of the request.

0 likes
10 replies
pmall's avatar

@MeiR_ct Why cant you move the files from public_html to the laravel public directory ?

MeiR_ct's avatar

@pmall I prefer to avoid this approach. There are plenty of scripts and sites which weren't created by me, and I have no way to know the dependencies and linking inside them. There might be absolute paths defined in several places, and massively moving everything can break things .

bashy's avatar

If you don't want to move your existing files, move Laravel up a level then?

MeiR_ct's avatar

@bashy I'm exploring different methods for that exactly right now. Most of them involving changes of plenty of paths, but I just found a tutorial that suggests a smarter way:

https://mattstauffer.co/blog/extending-laravels-application

According to this, I should override "publicPath" function to return the path to "public_html".

The problem is I'm not familiar with Laravel's routing and I'm not sure what will happen if I do that. Can you or someone else please look inside the auto-created ".htaccess" in laravel/public, and to approve that everything will work? I cannot make trial and error since it may affect the other websites.

pmall's avatar
pmall
Best Answer
Level 56

You may change the path of laravel public folder by putting this in your AppServiceProvider register method :

$this->app->bind('path.public', function () {

    return base_path() . '/public_html';

});
1 like
bashy's avatar

@MeiR_ct And this is why you shouldn't do absolute paths in code.

There might be absolute paths defined in several places, and massively moving everything can break things.

Should be using things like this

dirname(__FILE__) . '/file.php';
MeiR_ct's avatar

@bashy as I mentioned, those existing sites weren't created by me. The company had several web apps before I started to work there.

@pmall thanks for the additional method. From what I understand, the defined "public" directory must contain the special ".htaccess" which is automatically created by Laravel during installation, and handles the routing.

I would like to know how it will affect the responses for all the files and subdirectories that will be in that location.

pmall's avatar

@MeiR_ct no, the only thing this htaccess do is to check if the requested url match a file in the directory. If so it serves the file, if not the request is handled by laravel's index.php.

MeiR_ct's avatar

@pmall great! Sounds like exactly what I need :) I'll try that in few days and will reply here if I run into any issues.

Thanks for the help!

Please or to participate in this conversation.