Rewrite URL with parameters

Jonathan Matthews contact at
Thu Dec 5 17:24:46 UTC 2013

On 5 December 2013 17:09, Raphael R. O. <rabeloo at> wrote:
> Hi guys,
> I'm trying to rewrite an url with a few parameters, but unsuccessfully.

You're almost there ;-)

> What i already tried:
> rewrite
> ^/category-body/categories/promotional(.*)utm_source=PromoCode&utm_medium=AddPromo&utm_campaign=PromoCode_AddPromo_moneycampaing;

"rewrite" doesn't examine query strings, so if the contents of your
query string are required to drive your logic, you'll need to use
other tools instead of (or well as) rewrite.

> and ...
> location = /category-body/categories/promotional(.*)$ {

You probably don't want the "=" here. I don't actually know what a
regex ("(.*)") location used alongside "=" will do. I'm slightly
surprised nginx didn't complain on reload/restart ...

Have a read of this section:

You probably want to use a case-sensitive regex location.

> if ($args ~
> “utm_source=PromoCode&utm_medium=AddPromo&utm_campaign=PromoCode_AddPromo_moneycampaing”)

I'd use a map{} variable inside the if(), personally. The map can use
the same check as that which you have above, but in a way which
abstracts the actual check away from the logic that it drives. This is
just a stylistic change however :-)


More information about the nginx mailing list