NGINX getting an application in a subfolder to work with redirects

Solution 1:

You want URIs that begin with /m to be handled by the location /m block.

However, regular expression locations take precedence, so the URI /m/form/generate.js is actually being handled by another location that matches all URIs that end with .js. See this document.

There is the ^~ operator which forces the prefix location to take precedence, but that will not work in your case, as the application is PHP, and you need URIs that end with .php to be handled by the location ~ \.php$ block.

So instead, I suggest you nest the location causing the problem within the location / block.

For example:

location /m {
    try_files $uri $uri/ /m/?q=$uri&$args;
}

location / {
    try_files $uri $uri/ /index.php$is_args$args;

    location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
        access_log        off;
        log_not_found     off;
        expires           360d;
    }
}

location ~ \.php$ {
    ...
}

location ~ /\.ht {
    ...
}