0

I had a bunch of urls like so:

http://domain.com/l/key-a=value-a/key-b=value-b.html

They were indexed by Google, but have since changed in our system from = signs to - signs, so I would like to redirect request like the one above to:

http://domain.com/l/key-a-value-a/key-b-value-b.html

Note: there could be one or more key-value param sets, the above is just an example.

What's the best way to do this in NGINX?

Nathan
  • 7,627
  • 11
  • 46
  • 80
  • Possible duplicate of [How to replace underscore to dash with Nginx](http://stackoverflow.com/questions/15912191/how-to-replace-underscore-to-dash-with-nginx) – CarHa Jul 16 '16 at 13:47
  • Similar, only thing that's problematic is that it's an "=" sign, and that can be used in other cases. I need to rewrite it only if the params are prefixed with the "/l/" path. Do you know how I might do that? – Nathan Jul 17 '16 at 16:52
  • Does the path contain key-value sets only? What is the maximum number of sets? Is 'key' a keyword or just a placeholder? – CarHa Jul 18 '16 at 03:27

1 Answers1

0

I was able to solve this using the following block:

  location ~ /l/(.*)=(.*) {
    rewrite ^([^=]*)=(.*)$ $scheme://$host$1-$2;
    return 301;
  }

This seemed to work even when there are multiple key-value pair segments in the the path, ex:

domain.com/l/color=red/size=large/shape=round

UPDATE:

I discovered that in cases where multiple key-value segments were present, there were multiple 301 redirects, and Google doesn't like this.

So I ended up going with:

  location ~ /l/(.*)=(.*) {
    rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5-$6-$7-$8-$9 permanent;
    rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5-$6-$7-$8 permanent;
    rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5-$6-$7 permanent;
    rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5-$6 permanent;
    rewrite ^([^=]*)=([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4-$5 permanent;
    rewrite ^([^=]*)=([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3-$4 permanent;
    rewrite ^([^=]*)=([^=]*)=(.*)$ $scheme://$host$1-$2-$3 permanent;
    rewrite ^([^=]*)=(.*)$ $scheme://$host$1-$2 permanent;
  }

The decreasing powers of 2s example (How to replace underscore to dash with Nginx) did not work for me.

Community
  • 1
  • 1
Nathan
  • 7,627
  • 11
  • 46
  • 80