Rewriting URL params in nginx

I came across this problem recently, a customer was moving to Ruby on Rails from another framework/language (.NET I think) and needed to re-write a bunch of URLs. Some needed the query parameters rewriting too. One example was rewriting the old search path, so the old URL:

http://somedomain.com/OldSearchPath.aspx?qry=things&page=4

would become:

http://somedomain.com/search?query=things&page=4

This should be fairly simple except for the qry parameter needed to be changed to query. A bit of googling didn’t turn up much but with some experimentation I came up with this using the pre-populated nginx $args variable:

location /OldSearchPath.aspx {
  if ($args ~* qry=(.+)) {
    set $args query=$1;
  }
}
rewrite ^.+$ /search redirect;

It even leaves the other parameters intact, so the pagination will still work.

Tagged ,

One thought on “Rewriting URL params in nginx

  1. [...] found a few different inspecific references, but here was the upshot. Each HTTP variable is available in the [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.