Search code examples
nginxconfigurationroot

Setting up nginx to redirect to the root of the domain on directory browsing


Can we set up nginx to redirect to the root of our domain if the user attempts to browse any directories that do not have index.php or index.html? I have this in my conf file, but now it seems to be blocking valid page access:

location /* {
    deny all;
    return 301 /;
}

Solution

  • The default nginx behavior is to throw 403 Forbidden on such directories unless you have autoindex on; directive in your config or change default behavior with custom try_files directive. The simplest thing you can do is to override this error with redirection to your site root URI:

    error_page 403 =301 /;