How to hide config files from direct access?

13,733

Solution 1

You're using wrong web server configuration. Point your web server to a public directory and restart it.

For Apache you can use these directives:

DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">

For nginx, you should change this line:

root /path_to_laravel_project/public;

After doing that, all Laravel files will not be accessible from browser anymore.

Solution 2

Point your web server to a public directory and restart it.

For Apache you can use these directives:

DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">

Also You Can Deny files in .htaccess too.

<Files "composer.json">
Order Allow,Deny
Deny from all
</Files>

for multiple files you can add above files tag multiple times in .htaccess files.

Solution 3

That is incorrect. composer.json sits outside of the public directory and therefore should not be accessible. This means that your VirtualHost configuration is incorrect.

Please make sure that your path to your directory ends with /public.

Solution 4

Point the web server to the public directory in the project's root folder

project root folder/public

but if you don't have the public folder and you are already pointing to the root folder, you can deny access by writing the following code in .htaccess file.

<Files ".env">
Order Allow,Deny
Deny from all
Allow from 127.0.0.1
</Files>

in the above code, first we are denying from all and allowing only from the own server (localhost to the server) to get executed, and hence we can protect it from outside users.

Share:
13,733

Related videos on Youtube

Yasen Ivanov
Author by

Yasen Ivanov

Updated on June 15, 2022

Comments

  • Yasen Ivanov
    Yasen Ivanov almost 2 years

    I am using Laravel for web app. Uploaded everything on production and found out that some of the files can be directly accessed by url - for example http://example.com/composer.json

    How to avoid that direct access?

  • Yasen Ivanov
    Yasen Ivanov almost 8 years
    I am using Apache. Ok, but I don't want this public folder to appear in my URL. If I set my document root to public, I am bound to use /public to my url's..
  • Chris
    Chris almost 8 years
    @YasenIvanov I don't understand what your complaint is at all. Surely at the moment you have to use /public/ at the start of your URLs? If you set your document root to the public directory then you won't. Why do you want to have /public/ at the start of all your URLs?
  • Yasen Ivanov
    Yasen Ivanov almost 8 years
    I don't want to have /public/ to my URLs - that's what I've said. If I set my document root to public directory it just doesn't allow me to get acces like this - domain.com - it says "Forbidden". If I type it domain.com/public I have access. The question is how to remove this /public/ from my URL's ?
  • Giedrius Kiršys
    Giedrius Kiršys almost 8 years
    @YasenIvanov I think, that You understood me and Chris wrong. When You set document root to public path, it will be Your root directory for web server, so no more /public/ in URL.
  • Edd
    Edd about 3 years
    This would not deny access to composer.json. It just hides the directory listing. To disable directory listing, add Options -Indexes to the .htaccess file.

Related