Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Using Morph Labs' Appspace to deploy a site means no automated way to redirect 'myapp.com' to 'www.myapp.com' (and no access to .htacess).

Is there an in-rails way to do this? Would I need a plugin like subdomain-fu?

More specifically, I'm trying to do something like:

  • 'myapp.com' => 'www.myapp.com'
  • 'myapp.com/session/new' => 'www.myapp.com/session/new'

Basically, I always want the 'www' subdomain prepended on every request (because the SSL cert specifically has a common name of 'www.myapp.com').

like image 446
Allan L. Avatar asked Nov 29 '08 03:11

Allan L.


1 Answers

Maybe something like this would do the trick:

class ApplicationController < ActionController::Base   before_filter :check_uri    def check_uri     redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host)   end end 
like image 108
carson Avatar answered Sep 19 '22 15:09

carson