Browse files

redirect www to top level domain

  • Loading branch information...
1 parent 01d9088 commit e8b1d34251d9e2c269efcefc9a6f50aff7e30c80 @stevenwilkin committed Jan 30, 2012
Showing with 21 additions and 0 deletions.
  1. +4 −0 config.ru
  2. +17 −0 lib/no_www.rb
View
4 config.ru
@@ -1,3 +1,7 @@
+require './lib/no_www'
+
+use NoWWW
+
use Rack::Static, :urls => ['/css', '/img'], :root => '.'
run lambda { |env|
View
17 lib/no_www.rb
@@ -0,0 +1,17 @@
+class NoWWW
+
+ STARTS_WITH_WWW = /^www\./i
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ if env['HTTP_HOST'] =~ STARTS_WITH_WWW
+ [301, { 'Location' => Rack::Request.new(env).url.sub(/www\./i, '') }, ['Redirecting...']]
+ else
+ @app.call(env)
+ end
+ end
+
+end

0 comments on commit e8b1d34

Please sign in to comment.