---
title: Wildcards In Rails Redirects
teaser:
tags: web,rails
author: Dan Croak
published_on: 2012-02-15
---

The captured wildcard in a Rails 3 route can be used in the redirect method:

    match 'via/:source' => redirect('/?utm_source=%{source}')

This example is intended to improve metrics for customer acquisition campaigns.
`utm_source` is for Google Analytics, which [KISSMetrics logs as Ad Campaign
Hits](http://support.kissmetrics.com/integrations/utm-variables).

The URLs are now friendlier for sharing:

    /via/email
    /via/twitter
    /via/search-ads
    /via/blog-ads

When the user clicks them, they'll be redirected to:

    /?utm_source=email
    /?utm_source=twitter
    /?utm_source=search-ads
    /?utm_source=blog-ads

The URLs are also encapsulated. If the Google Analytics params need to change,
the developer can edit `config/routes.rb` and deploy. All past routes will still
work.
