Search code examples
regexnginxhttp-redirectwebserver

NGINX redirect everything except one directory and its subdirectories


How do I need to modify the code that everything redirects to another website except the directory "l"? Also, all like e.g. /l/test shouldn't redirect.

location / {
    return 301 https://www.example.com$request_uri;
}

location ~ /l {
    root /var/www;
    rewrite ^(.*)$ /index.php?l=$1 last;
}

Solution

  • With that configuration its works!

    root /var/www/;
    
    location / {
            return 301 https://www.example.com$request_uri;
        }
    
        location /l/ {
            rewrite ^/l/(.*)$ /index.php?p=$1 last;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            #...
        }