Nginx: String Replace plus sign with hyphen

nginxrewrite

I am trying to rewrite hundreds of URLs, and I've gotten pretty close except for one scenario.

Some URLs look like this:

/city/?sl_city=CityName&directory_nonce=45ad967e02

But some have a + as a word separator in the sl_city query var. Those need to be changed to a -

/city/?sl_city=Some+City&directory_nonce=45ad967e02

The final outcome for these two URLs should be like:

/locations/cityname
/locations/some-city

My basic rewrite rule is this:

location ~* ^/city/(.+)? {
    if ($args ~* "sl_city=(.+)&directory_nonce=(.+)") {
      set $city $1;
      set $args '';
      rewrite ^.*$ /locations/$city permanent;
    }
}

But my second URL 404s (comes back as /locations/Some+City), but locations/Some-City resolves. I am not worried about casing for these URLs at the moment.

Basically I want to check $city before it does the final permanent rewrite, and any + found, change it to a -.

All the examples I have seen involve installing another module, which I don't think I can do. And my attempts so far haven't worked.

EDIT

Here's the modules I have installed. Trying to figure out if ngx_http_substitutions_filter_module does what I need, but don't know.

nginx version: nginx/1.8.0
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-dav-ext-module --add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.8.0/debian/modules/ngx_http_substitutions_filter_module

Best Answer

This will replace the plus with a hyphen for you:

location ^~ /city/ {
    # For the single word case, just copy the city name
    set $city $arg_sl_city;
    # If the city is two words, convert + to "-"
    if ($arg_sl_city ~* "(\w+)[-+](\w+)") {
      set $city "$1-$2";
    }
    set $args '';
    rewrite ^.*$ /locations/$city permanent;
}

However, lowercasing the city names requires a module like embedded perl. That is, unless all the possible city names are known and a reasonably-sized list. Then you could use a map directive to statically map the upper-case versions to lower-case versions.