Search code examples
.htaccessserverdirectoryhttp-status-code-404status

How to make 404 error on homepage?


I have an empty domain, and would like to return a 404 error when people go to the homepage, or any other URL. Currently, when I go to the domain, I'm being displayed the empty directory.

How can I achieve that?


Solution

  • I'm being displayed the empty directory.

    I assume you mean you are being shown an (Apache generated) index of that directory (which would appear to be empty). This is a "feature" of Apache. To disable directory indexes then include the following at the top of the .htaccess file in your document root:

    Options -Indexes
    

    However, this will result in a 403 Forbidden being served when accessing a directory that does not contain a DirectoryIndex document (because server-generated directory indexes are "forbidden"). To explicitly return a 404 for all requests, include the following below the above in the .htaccess file:

    RewriteEngine On
    RewriteRule ^ - [R=404]
    

    This uses mod_rewrite to instruct Apache to return a 404 status (default error page) for all requests.

    However, this does assume that you have not defined a custom 404 ErrorDocument (otherwise you'll get a 500 error resulting from a rewrite loop).