Remove first part of path, if = a particular string?

Hi all. I’m moving our site from eg /staging/ to
simply be
/<rest_of_path>. I want to catch all the people who have
bookmarked the “staging/” urls (or have them in their browser history)
and redirect them to the equivalent url without “staging/”. Eg if
someone goes to

/staging/foos/123/edit

it will send them to

/foos/123/edit

Is this something i can do in routes.rb? Or is it better done at the
server config level?

thanks, max

Hi Max,

On Mon, Jun 28, 2010 at 6:51 AM, Max W. [email protected]
wrote:

/foos/123/edit

Is this something i can do in routes.rb?

Easily, with named routes.

Or is it better done at the server config level?

I’m pretty sure you could do it with mod_rewrite rules. And that
would keep your routes.rb smaller, so ‘better’ in that sense. OTOH,
it’s probably a worthwhile effort to do it with routes first, just to
make sure you identify any edge cases. My advice would be to make the
final decision based on your Apache config experience / comfort level.
Mine’s minimal :wink:

Best regards,
Bill

Bill W. wrote:

Easily, with named routes.

Thanks Bill - would you mind explaining your easy named routes solution?
:slight_smile:

Hi Max,

On Mon, Jun 28, 2010 at 7:18 AM, Max W. [email protected]
wrote:

Bill W. wrote:

Easily, with named routes.

Thanks Bill - would you mind explaining your easy named routes solution?
:slight_smile:

Without knowing more about your Rails version and existing routes it’s
a bit tough to give you ‘the’ solution, but as a starting pointI’d try
‘simply’ installing some new default routes.

map.connect ‘/staging/:controller/:action’
map.connect ‘/staging/:controller/:action/:id’
map.connect ‘/staging/:controller/:action/:id.:format’

At the other end of the tedium spectrum, you could put a named route
with /staging added to the beginning of the url in place for each of
your existing routes. Run ‘rake routes’ for a list.

Sorry I don’t have more time today. It’s Monday, for sure ;-). There
are good online resources for routing. I’d recommend starting with

HTH,
Bill

Hi Bill - thanks. That had occurred to me too, but I was hoping to not
have to go through and individually catch every possible route and
individually redirect each one. The problem with this as well is that
it lets people carry on using the staging urls, silently sending them
through to the right action.

What i did in the mean time, and i might stick with this, is just catch
missing route exceptions in application_controller and, if they start
with “staging/” just redirect users to the home page with a flash
message. That way at least they get redirected to the proper home page
url, from which they can start building up a more appropriate browsing
history again.

thanks for your help, max

Max W. wrote:

Hi Bill - thanks. That had occurred to me too, but I was hoping to not
have to go through and individually catch every possible route and
individually redirect each one. The problem with this as well is that
it lets people carry on using the staging urls, silently sending them
through to the right action.

What i did in the mean time, and i might stick with this, is just catch
missing route exceptions in application_controller and, if they start
with “staging/” just redirect users to the home page with a flash
message. That way at least they get redirected to the proper home page
url, from which they can start building up a more appropriate browsing
history again.

Simple:
map.connect ‘staging/*rest_of_path’ to some staging action

In your staging action, have some logic that sets the flash and
redirects to rest_of_path.

thanks for your help, max

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]