.htaccess on root directory to redirect to public folder

Published 1 week ago by dohisev

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.

ejdelmonico

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

dohisev

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

ejdelmonico

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

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

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

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

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

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

No need to change the .htaccess

dohisev

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

Snapey
Snapey
1 week ago (678,595 XP)

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

dohisev

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

@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
Snapey
1 week ago (678,595 XP)

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

Please sign in or create an account to participate in this conversation.