# Rewrite URL with parameters

**URL:** <https://www.ruby-forum.com/t/rewrite-url-with-parameters/237910>\
**Category:** NGINX\
**Created:** [December 5, 2013, 6:10pm UTC](https://www.ruby-forum.com/t/rewrite-url-with-parameters/237910 "2013-12-05T18:10:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [December 5, 2013, 6:10pm UTC](https://www.ruby-forum.com/t/rewrite-url-with-parameters/237910/1 "2013-12-05T18:10:31Z")

</div>

Hi guys,

I’m trying to rewrite an url with a few parameters, but unsuccessfully.

The URL:

[https://www.mysite.com.br/category-body/categories/promotionalXXX?utm\_source=PromoCode&utm\_medium=AddPromo&utm\_campaign=PromoCode\_AddPromo\_moneycampaing](https://www.mysite.com.br/category-body/categories/promotionalXXX?utm_source=PromoCode&utm_medium=AddPromo&utm_campaign=PromoCode_AddPromo_moneycampaing)

I need rewrite to:

[https://www.mysite.com.br/digital-parts/newspaper/recs-xyaw-sazz-qqad-cxae](https://www.mysite.com.br/digital-parts/newspaper/recs-xyaw-sazz-qqad-cxae)

The /promotionalXXX can be /promotional123, /promotional324 and many  
more  
…

What i already tried:

rewrite  
^/category-body/categories/promotional(.\*)utm\_source=PromoCode&utm\_medium=AddPromo&utm\_campaign=PromoCode\_AddPromo\_moneycampaing  
[https://www.mysite.com.br/digital-parts/newspaper/recs-xyaw-sazz-qqad-cxae](https://www.mysite.com.br/digital-parts/newspaper/recs-xyaw-sazz-qqad-cxae);

and …

location = /category-body/categories/promotional(.\*)$ {  
if ($args ~  
utm\_source=PromoCode&utm\_medium=AddPromo&utm\_campaign=PromoCode\_AddPromo\_moneycampaing)  
{  
rewrite ^/digital-parts/newspaper/recs-xyaw-sazz-qqad-cxae; permanent;  
}  
}

But any of this worked for me…

What’s the best way to solve this?

Thanks!

---

<div class="post-metadata">

**Author:** ![Raphael\_RSO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@Raphael\_RSO](https://www.ruby-forum.com/u/Raphael_RSO)\
**Post date:** [December 5, 2013, 6:25pm UTC](https://www.ruby-forum.com/t/rewrite-url-with-parameters/237910/2 "2013-12-05T18:25:08Z")

</div>

On 5 December 2013 17:09, Raphael R. O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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\_moneycampainghttps://www.mysite.com.br/digital-parts/newspaper/recs-xyaw-sazz-qqad-cxae;

“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:  
[http://wiki.nginx.org/HttpCoreModule#location](http://wiki.nginx.org/HttpCoreModule#location)

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 🙂

HTH,  
Jonathan
