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

towhid's avatar

public folder issue / .env file related issue

Hello , i want to know two things one things is

  1. in laravel project run without public directory
  2. when i hit .env / composer.json / package.json / robots.txt all file then show all information how to hide or what the procedure for this kind of problem

thank you

0 likes
32 replies
Sergiu17's avatar

What's the reason to put all files from public to root directory of applications?

edim's avatar

As the previous answer said it would be good to know

  1. What is the problem of having a public folder? I'm kinda assuming that it might have something to do with shared hosting? :D

  2. Set correct permissions on the file, are you using CPanel or are administrating some kind of linux based or windows machine?

Cronix's avatar
  1. No, don't. That is the only dir that should be accessible (/public) to the public. That's why it's called public. Everything else should not be accessible, like going up a dir and being able to access .env.
  2. Simply set your webservers DocumentRoot to the /public dir of laravel. That's how it's supposed to be, and you won't be able to read anything outside of the public dir. That's why it's called public. It's the only thing that should be exposed.

You'll have to figure out how to do that for whatever system/webserver you're using. There's lots of info on the net about setting up all webservers and the DocumentRoot.

1 like
towhid's avatar

@SERGIU17 - @sergiu17 i also undersatnd this thing only index file need to move but if i hit public folder like this path

http://localhost/laravel/laravel-one/public/css/app.css

then show all bootstrap css file detailss

towhid's avatar

@EDIM - ok i want to use local server , its happens there also ..

towhid's avatar

@CRONIX - but without public folder , if i route this link then show all information

http://localhost/laravel/laravel-one/.env

and

http://localhost/laravel/laravel-one/composer.json

and

http://localhost/laravel/laravel-one/package.json

all files are open -- :(

Sergiu17's avatar

@TOWHID - You don't have to move anything. You have to change settings of root directory to be /var/www/your-app/public, usually cPanels provide this in UI, it's very easy to change it.

jlrdw's avatar

Sorry above guide did not help, it does work:

Ok image:

https://i.imgur.com/Oo6k4Fp.jpg

laravel54up is main laravel above htdocs in wamp, shared hosting, xampp etc

laravel54 is public under htdocs.

I know of no other way to explain, thought a picture would help.

I was hoping the guide from @snapey would have helped.

public_html    htdocs    www   all the same depending on host

what the procedure for this kind of problem

Sorry if you do not understand.

Cronix's avatar

Because you haven't set the DocumentRoot to be /public. You need to learn how to set your webserver up.

Google whatever your webserver is plus document root. "nginx document root", "apache document root", or if you're using a control panel with your host, check how in the control panel, or with your host directly.

You will want to set up a virtualhost as well (so you can set document root up per domain), so you can access each project with it's own domain, like http://domain1.test, http://domain2.text etc, instead of /localhost/laravel/domain1, /localhost/laravel/domain2, etc.

Right now you have it setup so everything is being run off of a single domain, which isn't how you'd normally set up a website.

You'll want to read up a lot more on how to set up a virtual host for whatever it is you are using.

What did you use to set up your webserver? What webserver is it? Is it a WAMP type package?

towhid's avatar

@JLRDW - ok i will check it and i will confirm -- is it work me or not , thank you .

Cronix's avatar

Check the xamp docs. You have a lot of reading to do. You need to create a virtual host for each domain (each laravel project), and then properly set the document root for each domain. Google "xamp virtual host" for more info how to set it all up.

https://www.cloudways.com/blog/configure-virtual-host-on-windows-10-for-wordpress/

https://www.wpwhitesecurity.com/multiple-websites-xampp/

The above talks about creating wordpress sites, but that doesn't matter. It's the same for laravel. You just need to enter the correct DocumentRoot and ServerName for each domain, and then also add it to your hosts file.

I haven't used xamp for about 10 years, so can't really assist with this.

towhid's avatar

@CRONIX - ok i am trying your instruction, if i have any update i will inform you thanks

towhid's avatar

@CRONIX - hello , i created as like your instruction

http://laravel-one.localhost/.env

but its same result all files are open :( all arefile open .env ,package.Jason and composer.jasson

Cronix's avatar
Cronix
Best Answer
Level 67

We can only guess as to what you did since you didn't show your code/config. I assume you did not set it up correctly, or it would work. You've also been messing around with your files this whole time trying to get things to work in different ways. You may (probably) have broken it elsewhere when you were doing that. I really don't know what you've done or how you've set it up. There could be several places where you've broken it, and finding that will be quite a challenge, especially when you don't show us what you've done.

DocumentRoot should be /your/path/to/laravel/public. public needs to be the last part.

1 like
towhid's avatar

@CRONIX -

    <VirtualHost *:80>
    DocumentRoot "c:/xampp/htdocs/laravel/laravel-one"
    ServerName laravel-one.localhost
    <Directory "c:/xampp/htdocs/laravel/laravel-one">
    </Directory>
    </VirtualHost>

and

#
127.0.0.1 localhost
::1 localhost

127.0.0.1      laravel-one.localhost

Cronix's avatar

Did you read my last post, specifically the last sentence? Your DocumentRoot does not have /public

1 like
towhid's avatar

@CRONIX - @CRONIX - now

    <VirtualHost *:80>
    DocumentRoot "c:/xampp/htdocs/laravel/laravel-one"
    ServerName laravel-one.localhost
    <Directory "c:/xampp/htdocs/laravel/laravel-one">
    </Directory>
    </VirtualHost>

still route laravel-one.localhost/.env file ar open

Cronix's avatar

That's the same thing you posted before. I don't know what to tell you. You still haven't added /public to the end of DocumentRoot I've stated it 3 times now...

2 likes
Talinon's avatar

Hold on.. maybe I can help here.

From Laravel documentation:

After installing Laravel, you should configure your web server's document / web root to be the public directory. The index.php in this directory serves as the front controller for all HTTP requests entering your application.

Thread recap:

Simply set your webservers DocumentRoot to the /public dir of laravel

You have to change settings of root directory to be /var/www/your-app/public

Because you haven't set the DocumentRoot to be /public

DocumentRoot should be /your/path/to/laravel/public. public needs to be the last part.

Your DocumentRoot does not have /public

You still haven't added /public to the end of DocumentRoot

You might need to add public to your DocumentRoot?

2 likes
jlrdw's avatar

@TALINON - I think one of the problems that will come up, is setting up stuff one way in development and then having to set it up a different way if using shared hosting.

I try to match development and shared hosting environments.

Anyway this will be my last response here I promise.

The guide I shared is not from me it is just one that I use that works for both shared and local development.

1 like
edim's avatar

@TOWHID - Do it like this:

   DocumentRoot "c:/xampp/htdocs/laravel/laravel-one/public"
   ServerName laravel-one.localhost
   <Directory "c:/xampp/htdocs/laravel/laravel-one">
   </Directory>
   </VirtualHost>

the public was missing and that is why you NEED to have a public folder in your application. c:/xampp/htdocs/laravel/laravel-one/public

towhid's avatar

@cronix last night i was set as like your instructon

just type mistake and this is the code

 <VirtualHost *:80>
    DocumentRoot "c:/xampp/htdocs/laravel/laravel-one/public"
    ServerName laravel-one.localhost
    <Directory "c:/xampp/htdocs/laravel/laravel-one">
    </Directory>
    </VirtualHost>

and this time is not any file show its show ----- 404 error when i want to route

laravel-one.localhost/.env

and other's thing now tell me about shared hosting - local server solve my problem as like your instruction ........ i think this is my best answer for any local server ...

towhid's avatar

@TALINON - thanks for your instruction as like many way - its really helpful to me - :)

Cronix's avatar

If you think what he wrote was helpful, I'd urge you to read peoples posts a bit closer. His entire post was just quoting what everyone else had already said about adding /public to DocumentRoot, but you kept ignoring.

If you get en error for .env now, that's good. Does everything else work now? It's not clear by your post if things are all working now.

towhid's avatar

@CRONIX - i am not ignoring the points i just don't understand what all are said yes now everything is ok local server public folder directly rout and root folder not a single file open thank you .

Next

Please or to participate in this conversation.