1

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;
    }


}
  • [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – BMitch Oct 09 '22 at 14:07
  • You should probably remove `http://localhost` from the [rewrite replacement](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite) as that will force an external redirect. But the main problem from the error message is that the file `/QUERY/index.html` is not found at `/usr/share/nginx/html/QUERY/index.html` - are you expecting the request to be handled by your main `index.html` file? – Richard Smith Oct 09 '22 at 15:13
  • @RichardSmith I am expecting to get the results of the original query, which is handeled by index.html. Instead, nginx tries to find it in /usr/share/nginx/html/QUERY/index.html. My goal is for the format /QUERY/index.html be for aesthetic only, and not an actual redirection – Aviv Levintal Oct 09 '22 at 15:32

0 Answers0