code_junkie

Redirect 'myapp.com' to 'www.myapp.com' in rails without using htaccess?

Nov 12th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. class ApplicationController < ActionController::Base
  2. before_filter :check_uri
  3.  
  4. def check_uri
  5. redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host)
  6. end
  7. end
  8.  
  9. before_filter :check_uri
  10.  
  11. def check_uri
  12. if /^www/.match(request.host)
  13. redirect_to request.protocol + request.host_with_port[4..-1] + request.request_uri
  14. end
  15. end
  16.  
  17. head :moved_permanently, :location => ‘http://www.newdomain.com’
  18.  
  19. def rails_301
  20. headers["Status"] = "301 Moved Permanently"
  21. redirect_to "http://www.newdomain.com"
  22. end
  23.  
  24. def check_uri
  25. redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host) if Rails.env == 'production'
  26. end
Add Comment
Please, Sign In to add comment