• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

NGINX Rewrite


Well-known member
Can someone help me with a nginx location rewrite.

I've currently got a folder called directory with lots of files and sub folders in there.

I want to redirect everything in this folder to the root of the folder, So that if they vist www.site.com/directory/sheffield/varsity/ they end up at ww.site.com/directory/

Is this even possible?


New member
Not completely sure what you meant, but have a look:

server {
    listen      80;
    server_name www.site.com;
    root  /path/to/root;
    location /directory/sheffield/varsity/ {
        return 301 $scheme://www.site.com/directory/;
See how that works for you. It uses 301 Moved Permamently, hardcoded for now. You can also use this:

    location /directory {
        autoindex off;
That way, if someone accesses /directory, they can't see the file list there unless they know a direct link. You can also use "internal" or "allow deny all" if you need to completely remove permissions.