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

dohisev's avatar

.htaccess on root directory to redirect to public folder

As I'm in a shared hosting and I can't create a apache virtual host to point to the public folder I tried to use the solution of creating a .htaccess on the root folder to point to the public but it doesn't work, I'm getting an error: Sorry, the page you are looking for could not be found.:

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

How can I solve this? I also trying to get all the files from the public to the root folder but the css and js files are not loading properly using mix.

0 likes
15 replies
ejdelmonico's avatar

What is the root directory in the shared hosting? Is it www or public_html?

dohisev's avatar

public_html, but Laravel is currently in a folder called panel so it's public_html/panel

ejdelmonico's avatar

Well I have never tried two levels deep but maybe adjust these:

RewriteCond %{REQUEST_URI} !panel/public/
RewriteRule (.*) /panel/public/ [L]
dohisev's avatar

Just a question, you want me to put this on the /public_html/.htaccess? If yes, I can't cause I'm not allowed to change the root .htaccess as there are other projects running in other folders and there are rules in that .htaccess. The .htaccess I sent you was located inside /public_html/panel/.htaccess that would point to /public_html/panel/public.

ejdelmonico's avatar

Hmm, well then I think you are going to have a tough time trying to figure that one out. The only suggestion I would have is to place the Laravel site in a subdomain and link it via the root htaccess. That will allow all sites to run. You can even rewrite the url so no one notices.

dohisev's avatar

Ok, I created a subdomain and now I have a folder inside my public_html that is panel I should put your .htaccess code inside the public_html or public_html/panel?

alhoqbani's avatar

When you create the subdomain set the document root to public_html/panel/public

No need to change the .htaccess

dohisev's avatar

@alhoqbani How to do that? I'm a real newbie with those hosting things. Is it possible to do that using cpanel?

Snapey's avatar

can you not create files at the same level as public_html ?

the methods discussed here are dangerous and put you at risk of having your .env file downloaded

if you can create files at the root level, copy all your framework there and copy public folder contents into public_html

munyamakudzai095's avatar

@Snapey hey I need your help. I am trying to host a site on cpanel but outside the public_html directory soo I created a new dir in the same level as public_html and in the .htaccess I added a rule to redirect to the new dir with the laravel project but I get a 500 error.

RewriteEngine On
RewriteBase /

RewriteRule ^$ laravel/public/index.php [L]
RewriteRule (.*) laravel/public/ [L]
Snapey's avatar

@munyamakudzai095 delete public_html folder and create a symlink called public_html that points to your project public folder. That works 100%

dohisev's avatar

I solved setting the document root on cpanel subdomain configuration and it's working properly, the only problem now is that I'm getting error with permissions saying that can't access the log files. I don't want to set 777 permission because I saw that it's not safe.

ejdelmonico's avatar

@snapey how is it dangerous to place the project in a subdomain and redirect to that subdomain with htaccess? I must be missing something if that is the case.

Snapey's avatar

it's dangerous if your .env file is anywhere in your public space (even with redirects)

try googling filetype:env if you want to see some seriously misconfigured sites. It does not take long to find database, email and aws credentials

mohasin-dev's avatar

create .htaccess file in your root folder then page the code bellow in the file.

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

Please or to participate in this conversation.