Skip to content

omnifroodle/Route301

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Route301
========

A simple Rack middleware to allow mapping old paths to new websites.  Based partially on Rack::ReWrite.

The goals
---------------
* Catch lost links (usually established in an older incarnation of your website...)
* Preserve SEO by clearly communicating resource moves
* Freedom from URI shattering behaviors like ending paths in .php or .aspx which have nothing to do with the content
* Be simple
* Be flexible

Why not something like mod_rewrite
----------------
* don't do the work until it needs to be done (R301 only runs after your app gives up)
* returning the right content on a bad url only extends the confusion and wasted overhead
* returning the right content on a bad url (as well as a good one) might make your site look worse to search engines
* Google actually might respect 301's and you get to keep all your previous mojo
* because this makes sense for your problem

Why not this
----------------
* because you'd rather just do an after_filter in Rails
* because this doesn't fit your problem (like temporary redirects you'd like to fire before your app runs)

When setting up routes:
----------------

 :when should be a string or regular expression that matches the route you want to work with.

 :send_to should be a string (or a proc, see the example) where you want to permanently redirect traffic

See the example below for a simple string match and a regex match using a proc.  Your proc will receive a copy
of the server environment variables, and has full access to ActiveRecord for doing complex name resolution.
 
Example
=======

example initializer for Rails App, you could add this as config/initializers/route301.rb

ActionController::Dispatcher.middleware.use 'Route301', {:routes => [
  {:when => '/motels.aspx', :send_to => '/motels'},
	{:when => /motels\.aspx.*/, :send_to => proc{|env| motels_path(env)} }
]}

def motels_path(env)
  puts env['QUERY_STRING']
  params = Hash[env['QUERY_STRING'].split('&').map{|kvp| kvp.split('=')}]
  return "/motels/#{Motel.find_by_archive_id(params['q'].to_i)}"
end


Some Common things to do
-----------------
* store the old "?q=" id in your database and send people to your new pretty urls
* catch old static routes like "mystuff.asp" and send it to "mystuff"
* handle deprecated routes

Some less common things to do
----------------
* catch requests for missing images and create stubs for them during testing
* catch "broken" url's from the dreaded email newline and write a proc to recover based on the first half of the url
* set up honey-pots to catch people probing your server, and log it send a notification

Suggestions, questions, and comments please! @omnifroodle

Copyright (c) 2010 [Matt Overstreet], released under the MIT license

About

A low impact way to catch old uri's using Rack

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages