4

I have a url.map containing a rewrite rule

~^/A/shopby/(?<brand>[a-zA-Z]+) ~^/category/by-brand/$brand;

so that /A/shopby/a_brand will be redirect to /category/by-brand/a_brand

And also a config file

map $request_uri $new_uri {
  include /etc/nginx/urls.map;
}

server {
  rewrite_log on;
  error_log /var/log/nginx/error.log notice;

  if ($new_uri) {
     # return 200 $new_uri; for debugging
     rewrite $request_uri $new_uri permanent;
  }

 location / {
   root /a_root;
   index  index.html;
   try_files $uri $uri/ /index.html;
 }
}

And I keep getting this *60 "$request_uri" does not match "/A/shopby/a_brand" and also my debugging only returned me ~^/category/by-brand/$brand which showed regex didn't replace the captured string

What did I miss in the process? Any help would be greatly appreciated

Chung
  • 182
  • 1
  • 1
  • 7

1 Answers1

1

Your rewrite statement is incorrect, as the first parameter should be a regular expression and not a variable.

But you do not need to use rewrite when you are changing the entire URI. A permanent rewrite is equivalent to a return 301.

For example:

if ($new_uri) {
    return 301 $new_uri;
}

If you need to pass the original arguments (which rewrite would do by default), use:

if ($new_uri) {
    return 301 $new_uri$is_args$args;
}

See this document for details.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29