is there any way to set a default page for all folders on server that don't have an index file?
Like a general 404 page?
The idea is to prevent browsing through folders with no index file
Can this be achieved with .htaccess or php?
Put
ErrorDocument 404 /path/to/file/that/handles/404/errors.php
in your httpd.conf
However, if you're wanting to prevent folder browsing, this won't help. You'll have to disable folder browsing in the first place:
Options -Indexes
Related
I have the following folder structure:
+users
-adduser.php
-viewuser.php
When a visitor navigates to example.com/users it's showing the folder structure. I need to restrict visitors ability to see the file listing, either by hiding it or removing it. How can I do that in php?
If you don't want to (or can't) deal with Apache configuration, create an empty file named index.html (or index.php if you prefer) in your users folder.
You can create a .htaccess file on root folder with following;
RewriteEngine on
Redirect 301 /users http://www.example.com/404.html
Then you just need to create 404.html in your website and change example.com with your domain.
Now your /users path will redirect to 404.html.
You can add the following to your Apache virtual host file:
<Directory "path_to_folder">
Options -Indexes
</Directory>
The above rule will disallow directory listing. It was suggested in this post: Using .htaccess, prevent users from accessing resource directories, and yet allow the sourcecode access resources
Ok so my current sites is on the .htaccess method to block user access to the directory
e.g. http://www.example/_directory/ via Options All -Indexes
Question should I stick with that or is putting an index file e.g. index.php in every directory better? I'm thinking of an index.php that will redirect to the homepage rather than giving users an error 403 page.
Opinions?
It would be clever to build your web site in a way that these subdirectories also have content (e.g. about/ also shows some information, when about/history/ and about/our-company/).
If the directories contain only files, it's IMHO totally fine to just have a 403.
Answers to your questions might be very biased.
If you're on a Unix/Linux server, you don't need to have blank index files at all in your directories. Just create a .htaccess file and put the following code in it:
Code:
Options -Indexes
When anyone tries to access the contents of a directory that doesn't have an index file, they'll get a 403 error.
Ref : http://wildlifedamage.unl.edu/manual/mod/core.html#options
I am looking for a way to access a folder in a site that contains index.html or index.php .
But when I remove the index file, when I try to access list of contents from the url, I get
404 page not found
error ;
Is this possible to see contents of a folder with features that I said , if yes why ?
You could enable directory listing in your .htaccess file that you would put in the directory that you want to browse:
Options +Indexes
But the fact that you are getting 404 error might also mean that the directory doesn't exist at all on the server. So make sure it exists and has a .htaccess file inside it allowing directory browsing.
I have a website and right now users can just open a directory and see all files in it. How do I disable that?
I was thinking about the php.ini file, or generally any php configuration that might do the trick (it is an Apache server run with PHP).
Disable Indexes option in apache configuration.
Put this either in your httpd.conf or .htaccess in the root of the site.
Options -Indexes
This way, if the folder does not have an index page, the user will get a 403 Forbidden error.
Put a text file named .htaccess to your root-dir of your website or the dir you want to hide with following content:
Options -Indexes
I just wanted to know how to stop user from viewing folders without index pages using PHP or mod rewrite?
All of these answers are good but I will rate the one with the most votes.
Configure your web server to not automatically serve directory indexes.
Using Apache, this is done with the -Indexes option:
<Directory /web/docs/spec>
Options -Indexes
</Directory>
You can also put this Options -Indexes directive in an .htaccess file in the specific directory.
You don't need PHP for this. Just put a blank index.html in the directory.
By viewing folders, I assume there is a directory listing that you don't want visible. In that case, in your .htaccess file:
Options -Indexes
This can be done on a per-directory basis. See:
http://httpd.apache.org/docs/1.3/mod/core.html#directory