Usually, when a directory has no index.html file in it, apache just displays a page which says "Index of /subdir" at the top and lists all the files. How is it possible to instead of this show a custom error page? Preferably something I can put in my .htaccess File
2 Answers
By default there will be configuration line such as the following in your apache config:
DirectoryIndex index.html index.php index.cgi
This defines the order in which those files are searched for to present the content for a directory when that directory is requested without any file part. If not found, and the autoindex feature is enabled, you will get the directory contents listed.
However, it is also possible to define an absolute path for DirectoryIndex
, such as:
DirectoryIndex index.html /default/index.html
Now, if there is no index.html in a directory, the contents of /default/index.html (relative to the DocumentRoot) will be used, which I believe is exactly what you're asking for.
- 3,806
- 12
- 15
-
Oh my! That is brilliant – HBruijn May 07 '19 at 15:13
First of all: my pet peeve, quoted from from the manual on .htaccess files:
You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.
You can disable the autoindex generation by setting:
<Directory /var/www>
Options -Indexes
</Directory>
After that visiting an empty directory or directories without an IndexDocument will generate a generic error 403 Forbidden, rather than a directory listing.
You can then tune what gets displayed instead by setting the ErrorDocument
<Directory /var/www>
Options -Indexes
ErrorDocument 403 /errors/403.html
</Directory>
- 72,524
- 21
- 127
- 192
-
Thanks for the answer, but is there a way to seperate errors for 403 and a folder without an index.html? – OfficialCRUGG May 07 '19 at 14:17
-
No. Instead you could consider, rather than disabling auto-indexing, to customize the look and feel of the page that mod_autoindex generates and what gets listed there – HBruijn May 07 '19 at 14:32