Nginx (last/break) rewrites

nginxrewrite

I have a simple nginx rewrite here, using permanent flag, it works fine.

location /area1/ {
rewrite /area1/(.*)$ / permanent;
}

However i want to retain the initial url, which seems from research to just need the last or break flag instead.

location /area1/ {
rewrite /area1/(.*)$ / break;
}

After changing permanent for break (or last) , it just seems to ignore the redirect entirely.

Please can someone demonstrate a working internal redirect that retains the initial url – i know this should be simple but i have tried a ton of config variations around the above and nothing seems to work, redirect was more complex to start with but i have reduced it to its basic form just to get the syntax correct , and i'm still failing…

Among many pages i referenced, this is one – https://www.nginx.com/blog/creating-nginx-rewrite-rules/

server {
    listen        80;
    root         /usr/share/nginx/html;
    access_log   /var/log/nginx/root_host.access.log  main;
    error_log    /var/log/nginx/root_host.error.log;

    location /area1/ {
        rewrite /area1/(.*)$ / break;
    }

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
        rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
}

Best Answer

Your rewrite ... last doesn't really do anything.

You internally rewrite /area1/ to / which is then internally rewritten to /index.php by the last try_files statement.

Without the location /area1/ block present, /area1/ is internally rewritten to /index.php by the last try_files statement.