In my application is possible to create a directory, and the user can store files inside that folders. So suppose that I have the following structure:
/public_html
/app
/assets
/documents
/folder_name
-list of files
My goal is to prevent that the user types as URL:
https://myapp.com/assets/documents
or
https://myapp.com/assets/documents/folder_name
and see all the files or directories available in there, is there a way to prevent this via .htaccess?
If you want to simply prevent the directory listing, you can put:
Options -Indexes
inside an .htaccess file in the root directory of your assets. Your vhost/server configuration must have the AllowOverride Options set for this to work though.
If you wish to prevent the files from being accessed you could use the <Files directory which is allowed inside an .htaccess file, but the better solution would be to simply move that directory out of the public root.
Put .htaccess in that folder, and add
Deny from all within
Or if you want to apply the directive dynamically to folder (in case you have access to Apache configuration):
<DirectoryMatch "^/public_html/app/assets/documents/(.+)/" >
Order Allow,Deny
Deny from all
</DirectoryMatch>
or within the folder_name:
<DirectoryMatch "^/public_html/app/assets/documents/folder_name(.+)/" >
Order Allow,Deny
Deny from all
</DirectoryMatch>
One of the approaches is to generate blank index.php within a folder.
Related
I'm trying to hide all the php and some html file in my server folder only by direct call from url... particularly i need to hide the register.html (this file is for users registration), and other php file.. (these php files contain sensitive data for database connection). How can i do this?? Thanks for any help ;)
Create/edit the .htaccess file in the directory where the files are located.
For denying all direct access to all files in the directory, add this:
Deny from all
For restricting only a specific file:
<Files "register.html">
Order Allow,Deny
Deny from all
</Files>
For restricting specific file types, you need to edit the .htaccess file in the webroot folder. This rule will deny access to all .php and .html files in myFolder and its subdirectories.:
RewriteRule ^myFolder/.*\.(php|html)$ - [F,L,NC]
More examples on denying access to file types: https://stackoverflow.com/a/20489058/6817376
You can use this for register.html:
<Files "register.html">
Order Allow,Deny
Deny from all
</Files>
and in the same way for other files.
How do I prevent access to a given directory ? Here is the structure of my website :
config.php
.htaccess
classes/
|----lib/
| |----...
|----classes.php
webroot/
|----index.php
|----ctrl/
| |----...
|----pages/
| |----...
|----style/
|----...
The DocumentRoot is webroot. I want to prohibit access to the following folders : ctrl, pages, style. But the files stored in these folders still have to be available to be included in the index.php file.
In other words, there is an index.php file. Its role is to call (include) all the necessary files from pages/, ... The users can load index.php from their browser but they cannot access the other files alone.
I wrote this code is a .htaccess file but it doesn't do anything :
deny from all
<Files webroot/index.php>
allow from all
</Files>
This .htaccess file is located at the root folder for my project (before webroot/). What I try to do here is to deny access to every directory and files in the project except for the main index.php file.
What is it not working ? What went wrong ?
Thank you in advance.
I solved the problem. First of all, I had to activate .htaccess files in my httpd.conf file. Then I wrote this in my .htaccess :
<Files webroot/pages>
deny from all
</Files>
<Files webroot/style>
deny from all
</Files>
Do you want to prevent people list files in your webroot?
If so, you can add the following option in your htaccess file:
Options -Indexes
I am using Laravel 5.1
I recently uploaded my project in shared hosting. but when i browse http://siteAddress.com/local/.env my .env file is visible.
Is there any way to hide this file or redirect people if they want browse the site with folder view?
Finally I hide .env and disable index view of the folder named local. I create a .htaccess in folder local.
And here is the code of .htaccess
# Disable index view
Options -Indexes
# Hide a specific file
<Files .env>
Order allow,deny
Deny from all
</Files>
Please create a .htaccess file where you have .env file and write the code as shown below:
# STRONG HTACCESS PROTECTION
<Files ~ "^.*\.([Ee][Nn][Vv])">
order allow,deny
deny from all
satisfy all
</Files>
Then try to hit the .env file from url and it will not be available and show codes inside.
If you want to remove it from github.
Please create new file .gitignore on the same directory.
and add line
.env
You can add below code in .htaccess file to disable directory listing and restrict access of .env file:
# Disable Directory listing
Options -Indexes
# block files which needs to be hidden, specify .example extension of the file
<Files ~ "\.(env|json|config.js|md|gitignore|gitattributes|lock)$">
Order allow,deny
Deny from all
</Files>
The .env file resides outside the public folder so it should not be visible from outside world if the server is configured to see the public folder as document root.
From the best answer:
Remember that once your server is configured to see the public folder
as the document root, no one can view the files that one level down
that folder, which means that your .env file is already protected, as
well your entire application. - That is the reason the public folder
is there, security. - The only directories that you can see in your
browser if you set the document root to the public folder is the
folders that are there, like the styles and scripts.
https://laracasts.com/discuss/channels/general-discussion/how-do-you-protect-env-file-from-public
Check the folder structure on your hosting and make sure the public folder is the document root.
I have folder containing several files and sub-folders such as the following:
-/folder
-/subfolder
-/subfolder
-/subfolder
-/etc...
-index.html
-control.php
-ads.php
-/etc...
Now I want to disable work PHP or other files in main directory only (-/folder) like (-index.html, -control.php) , but all sub-folder files I want to works well.
I want to have effect on the files within that main folder only .
If I understand the question correctly, you need a .htaccess in the given directory with this content:
deny from all
Edit: Since as far as I know .htaccess rules apply to all subdirectories as well, you might have to create a .htaccess file in each subdir to override the restriction specified for root
allow from all
The configuration directives found in a .htaccess file are applied to
the directory in which the .htaccess file is found, and to all
subdirectories thereof.
http://httpd.apache.org/docs/current/howto/htaccess.html#how
You can use multiple directives to override the first directive
<directory /path/to/dir>
Order Allow,Deny
Deny from All
</directory>
<directory /path/to/dir/subdir>
Order Deny,Allow
Allow from All
</directory>
If you are using apache, you can add to your virtual-host the
<directory /path/to/dir>
Order allow,deny
Deny from All
</directory>
directive for each sub-directories, see http://httpd.apache.org/docs/2.2/howto/access.html
(I hope I understand your need)
Edit
if you need to filter files instead of directories, you can use a glob matching like that
<Files *.php>
Order allow,deny
Deny from All
</Files>
See http://httpd.apache.org/docs/2.2/en/mod/core.html#files
I am doing PHP web application, with Apache.
There are a few configuration files ( like App.yml) whose content I don't want to expose to users under whatsoever circumstances. Is there anyway that I can tweak my Apache setting so that these files won't be available when hostile users query for them?
The best option would be to place the files outside of your document root.
If that's not possible, you can deny access to them in apache .conf file (or a .htaccess file) with
<Directory /path/to/dir>
Deny from all
</Directory>
You can create a .htaccess file in that directory and place in it
order deny,allow
deny from all
You can also do this if you only want to block one file.
<Files filenamehere>
order deny,allow
deny from all
</Files>