will.thoughts.pop
RSS icon Email icon Home icon
  • Rewriting URL params in nginx

    Posted on March 7th, 2009 Will No comments

    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.