Ngnix rewrite won't behave as expected

40 Views Asked by At

I am trying to rewrite my urls from

http://localhost/?search=QUERY&ingrids=&_ingrids=&page=1

to

http://localhost/QUERY/index.html

I did manage to create a rule that rewrites the url, but it seems to redirect it instead of rewriting it. I read about the break keyword, but it doesn't seem to work...

I am adding 2 pictures of the error and my nginx configuration.

Nginx server configuration:

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    rewrite_log on;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    location / {
            if ($args ~* /?search=(.+)&ingrids=&_ingrids=&page=1) {
                rewrite ^ http://localhost/$arg_search/index.html? break;
            }
        }

    location /recipe/{
        try_files $uri /index.html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


}

Browse results Error message

Thank you!

UPDATE:

I did manage to make it work with manage to make it work with permanent, but it seems to result in blank page when I change the flag to last ( I want to avoid redirection)

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    rewrite_log on;

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    
  
    location ~* /.+/index.html {
        if ($uri ~* /.+/index.html){

            rewrite ^/(.+)/index.html$ /?search=$1&ingrids=&_ingrids=&page=1 last;
        }
    }

    location = / {
        try_files $uri $uri/ /index.html$is_args$args;
    }
        


    location /recipe/{
        try_files $uri /index.html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


}
0

There are 0 best solutions below