i'm trying to make a redirects from URL's with option ID to URL's with option URL_key i've assigned.

I have two column table with ID i'd like to substitute and URL_key as substitute.

11337 bar 11419 restaurant 11399 cafe 15477 painting

I have these URL's coming right now:


that i would like redirect to:


You may say i could just take all the urls from website and substitute with replace, but they are being generated automatically and each combination may be different from each other, so i would like to catch the ID and rewrite it to URL_key.

Any thoughts and solutions are appreciated.

We run our server on nginx, no apache.

  • nginx.org/r/map Commented Dec 25, 2014 at 16:13
  • @MichaelHampton thanks for this reference. As far as i know, map is not flexible enough to change part of url. it suits to rewrite full urls. Could you please give an example of how can i use it for my case?
    – Damir
    Commented Dec 25, 2014 at 16:19

1 Answer 1


This should work:

map $loc $loc_text {
    11337 bar;
    11419 restaurant;

map $style $style_text {
    15477 painting;

rewrite ^/art/kartiny/location/(?P<loc>\d+)/style/(?P<style>\d+)$ 

See also http://nginx.org/en/docs/http/server_names.html#regex_names

  • Hi, @Tobia, thank you for this solution, it seems like a working one, but i can include /art/kartiny here, it's other part of URL which i wouldn't like to monitor. I need to change only something like <br/> ^/location/(?P<loc>\d+)/style/(?P<style>\d+)$ /location/$loc_text/style/$style_text <br/> Is it possible? It would be awesome if it was possible to leave only something like <br/> ^/(?P<loc>\d+)/ /$loc_text/
    – Damir
    Commented Dec 26, 2014 at 7:32
  • You mean like this: rewrite ^/(.*)/location/(?P<loc>\d+)/style/(?P<style>\d+)$ /$1/location/$loc_text/style/$style_text ?
    – Tobia
    Commented Dec 26, 2014 at 16:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .