URI Substitue _ with - and + with space

Thomas Glanzmann thomas at glanzmann.de
Fri Jan 5 09:54:48 UTC 2018


Hello,
I would like to substitue '_' with '-' and '+' with ' ' in the $URI and
pass it to upstream server that can't handle _ and + in the URI (IIS).

Based on [1] I found a working solution, however I would like to know if
there is a more efficient way to do the same for example using lua?

location / {
        rewrite ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7-$8-$9;
        rewrite ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5;
        rewrite ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3;
        rewrite ^([^_]*)_(.*)$ $1-$2;

        rewrite ^([^+]*)\+([^+]*)\+([^+]*)\+([^+]*)\+([^+]*)\+([^+]*)\+([^+]*)\+([^+]*)\+(.*)$ $1%20$2%20$3%20$4%20$5%20$6%20$7%20$8%20$9;
        rewrite ^([^+]*)\+([^+]*)\+([^+]*)\+([^+]*)\+(.*)$ $1%20$2%20$3%20$4%20$5;
        rewrite ^([^+]*)\+([^+]*)\+(.*)$ $1%20$2%20$3;
        rewrite ^([^+]*)\+(.*)$ '$1 $2';

        proxy_pass https://upstream/;
}

[1] https://stackoverflow.com/questions/15912191/how-to-replace-underscore-to-dash-with-nginx

Cheers,
        Thomas


More information about the nginx mailing list