Nginx URL rewrite search?q=string (opencart) Nginx URL rewrite search?q=string (opencart) nginx nginx

Nginx URL rewrite search?q=string (opencart)


Nginx doesn't allow you to match against the query string, instead it has an $args variable and if -s. You can combine this to make the example work like this:

(updated with an nginx if else statement)

location /search {    if ($args ~ "q=(?<q>.*)?") {        rewrite ^ /index.php?route=product/search&filter_name=$q last;    }    # Else (if no search query entered from form)    rewrite ^ /index.php?route=product/search last;}

But since if -s are considered evil in nginx configs, you should use (in your case) the try_files:

location /search    try_files $uri /index.php?route=product/search&filter_name=$arg_q;}


The problem is the $ sign. It needs to beforehand in this situation. Solution:

rewrite ^/search?q=$(.*)$ /search.php?q=$1? last;