Pretty URL's

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

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.

On 22 February 2010 03:21, Aaron W. [email protected] 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

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
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 :slight_smile:
Michael

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.

On 22 February 2010 21:06, Aaron W. [email protected] 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! :slight_smile:

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#
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:

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 :slight_smile: