Force Rails Heroku application from subdomain.herokuapp.com to apex domain?

What is the correct way to submit subdomain.herokuapp.com to the apex domain of the application? This is done in order to avoid multiple domain names with the same content.

+5
source share
3 answers

https://github.com/tylerhunt/rack-canonical-host seems like the perfect choice for this. Leaving it here for everyone who has the same question.

+7
source

Quote from https://devcenter.heroku.com/articles/custom-domains

myapp.herokuapp.com , . , , HTTP 301 - . HTTP- , ; , - myapp.herokuapp.com.

"subdomain.herokuapp.com" before ApplicationController .

+6

, :

class ApplicationController < ActionController::Base

  before_filter :redirect_to_example if Rails.env.production?

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  private

    # Redirect to the appropriate domain i.e. example.com
    def redirect_to_example
      domain_to_redirect_to = 'example.com'
      domain_exceptions = ['example.com', 'www.example.com']
      should_redirect = !(domain_exceptions.include? request.host)
      new_url = "#{request.protocol}#{domain_to_redirect_to}#{request.fullpath}"
      redirect_to new_url, status: :moved_permanently if should_redirect
    end
end

domain_to_redirect_to, , domain_exceptions.

+2
source

All Articles