0

The padrino docs let you know how to do a before filter in the context of a controller, but I'm trying to do a global filter.

The problem I'm trying to solve is that my domain name should be forwarded to the www. version, but godaddy isn't doing it properly. So now I want to manually redirect in my code - and hence the global before filter.

Or should I be looking at 'middleware' instead?

Louis Sayers
  • 2,162
  • 3
  • 30
  • 53

3 Answers3

0

Try to use rack-rewrite gem

In your config.ru add something like

require 'rack/rewrite'
use Rack::Rewrite do
r301 %r{.*}, 'http://www.your-domain.com$&',
  :if => Proc.new { |rack_env| rack_env['SERVER_NAME'] != 'www.your-domain.com' }
end

Dont forget to include gem 'rack-rewrite' into your Gemfile.

Beer Brother
  • 514
  • 2
  • 6
  • 15
0

First result in google http://logbook.route19.com/post/9018495987/sinatra-redirect-www-to-non-www

Just make the opposite of that link. Try something like that :

before do
  redirect "www.#{request.url}" unless request.host =~ /^www/
end
jgburet
  • 535
  • 3
  • 15
-1

I've added my global before filters to app.rb. Not sure if this is the correct way, but it works just the same as adding the before filter in my controller.

In your app.rb file

before do
  # Code goes here
end