myapp.herokuapp.comから独自ドメインへリダイレクトする
herokuへ独自ドメインを適用してもmyapp.herokuapp.comのドメインはアクティブであり続ける。
そのため、www.customdomain-example.comのような独自ドメインを登録したあとでも、myapp.herokuapp.comでもアプリケーションへアクセスできるので、独自ドメインへリダイレクトするようにしてみる。
# application_controller.rb # -----------------略------------------- before_filter :redirect_to_custom_domain private def redirect_to_custom_domain if request.host == "myapp.herokuapp.com" redirect_to "www.customdomain-example.com"+request.path, :status => 301 end end
とすると、myapp.herokuapp.com/example/hogeのようなアドレスにアクセスした時も、独自ドメインへリダイレクトできるようになる。