# Pretty URL's

**URL:** <https://www.ruby-forum.com/t/pretty-urls/183880>\
**Category:** Rails\
**Created:** [February 22, 2010, 9:31am UTC](https://www.ruby-forum.com/t/pretty-urls/183880 "2010-02-22T09:31:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [February 22, 2010, 9:31am UTC](https://www.ruby-forum.com/t/pretty-urls/183880/1 "2010-02-22T09:31:16Z")

</div>

So I should start out saying that this is my first project in rails  
and I am really digging it.

What I want to accomplish is the ability to use a dash in my URL’s. I  
have done some poking around but have not come up with much. The only  
place I really need the dashes right now are in the action area. So  
my URL would look like:

[http://www.mydomain.com/content/site-map](http://www.mydomain.com/content/site-map)

Where “content” is my controller and “site-map” is a definition in  
that controller.

I know that what I will likely be looking for is something in the  
routing, but I cant seem to get that working either.

Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [February 22, 2010, 10:19am UTC](https://www.ruby-forum.com/t/pretty-urls/183880/2 "2010-02-22T10:19:52Z")

</div>

On 22 February 2010 03:21, Aaron W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I want to accomplish is the ability to use a dash in my URL’s. I  
> have done some poking around but have not come up with much. The only  
> place I really need the dashes right now are in the action area. So  
> my URL would look like:
> 
> [http://www.mydomain.com/content/site-map](http://www.mydomain.com/content/site-map)

If I may correct you; I’d guess that you don’t _need_ hyphens in your  
URL, you _want_ them, and needs/wants are very different things.  
There’s probably no reason why you can’t use an underscore instead to  
keep nicely with a Rails convention:

> [http://www.mydomain.com/content/site\_map](http://www.mydomain.com/content/site_map)  
> is just as readable, and possible, more SEO friendly (contentious  
> issue: hypen vs. underscore…)

Hypens are seen by Ruby as the “subtract operator”, and you can’t put  
an operator in a function name definition (which is what a controller  
action is). So you couldn’t do things like:

{:url =\> {:controller =\> :content, :action =\> :site-map } }

without Ruby throwing the dummy out of the pram, and even:

{:url =\> {:controller =\> :content, :action =\> “site-map” } }

would annoy Rails, but you should be able to do:  
{:url =\> {:controller =\> :content, :action =\> :site\_map, :page =\>  
“cool-stuff” } }  
or  
{:url =\> {:controller =\> :content, :action =\> :site\_map, :page =\>  
“cool-stuff”.intern } }

To use them in the action parameter with success, you would have to  
make sure you’ve re-written routes to map your hyphens to a different  
character, which you use in the names of the controller action  
methods… but _why_ would you go to all that work, risking one  
mistake breaking everything, when sticking to the convention makes  
your life so easy?

HTH 🙂  
Michael

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [February 22, 2010, 10:07pm UTC](https://www.ruby-forum.com/t/pretty-urls/183880/3 "2010-02-22T22:07:36Z")

</div>

Thanks Michael,

The reason I am _wanting_ to do this is purely vanity. I personally  
like hyphens more than underscores.

However, the _need_ comes in where I currently have pages pointing to  
the afore-mentioned “site-map”, and I do not want these links (perhaps  
on search engines or links to my site from others), to break upon  
launching the new site.

Is there a way to take incoming requests and route them to the new  
page while sending a nice 302 header? I can get over not having these  
legacy links remain the same, as long as I can nicely redirect users  
to the right spot.

I agree, that I would need to stay within the conventions of RoR (that  
is, in fact, what drew me from PHP). I am just trying to think bigger  
picture.

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [February 23, 2010, 9:35am UTC](https://www.ruby-forum.com/t/pretty-urls/183880/4 "2010-02-23T09:35:19Z")

</div>

On 22 February 2010 21:06, Aaron W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, the _need_ comes in where I currently have pages pointing to  
> the afore-mentioned “site-map”, and I do not want these links (perhaps  
> on search engines or links to my site from others), to break upon  
> launching the new site.

See, now _that’s_ a need! 🙂

Okay… I haven’t done that myself with Rails; in the past I’ve used  
.htaccess files in PHP sites, with long lists of the old URLs and  
their new equivalent… but that probably won’t work here, as Rails is  
handling all the requests (you could do that in the routes file…  
smelly though)

After quick Google, and recalling a post on here last night, I’d have  
a look at first catching the errors yourself nicely, correcting the  
url, and redirecting with 302:  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/2ec48068778d8842?hl=en#](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/2ec48068778d8842?hl=en#)  
redirect\_to :action=\> new\_action, :status=\>301

This doesn’t feel like the cleanest approach either though, so I’d  
suggest a bit of Googling… five minutes search for “rails redirect  
old url” gleaned me these potential solutions; there’s bound to be a  
few more around if you give it an hour’s work:

> **[GitHub - calvincorreli/redirect\_routing: Redirect Routing](https://github.com/calvincorreli/redirect_routing)**
>
> Redirect Routing. Contribute to calvincorreli/redirect\_routing development by creating an account on GitHub.

[http://almosteffortless.com/2007/02/15/redirect-old-urls-in-rails/](http://almosteffortless.com/2007/02/15/redirect-old-urls-in-rails/)

It may be there is no perfect approach, and you just want to use  
something as a stop-gap for a few months while sites with old links  
get updated - you’d almost want to show a “the page you’ve requested  
doesn’t exist any more… it’s moved… update your links… you’re  
being redirected…” view… but that’s your call 🙂
