简体   繁体   中英

How to remove subdomain from root path in Rails

I want to remove the subdomain from root path.

I tried adding :subdomain => false to the root command in routes.rb file without success: when I enter manually a subdomain in the URL, the subdomain stays and will not be removed.

Example:

my root is => lvh.me:3000
enter subdomain manually => xyz.lvh.me:3000 and hit enter then it remains the same

This is what I tried already in my routes.rb file, without success:

root :to => 'home#show', :subdomain => false or
root :to => 'home#show', :constraints => { :subdomain => false }, via: [:get]

According to this comment on rails github , in Rails >= 4 you need to use constraints to obtain this. Try this:

constraints subdomain: false do
  root to: 'home#show'
end

@dgilperez: your code works great, but I need to change in application controller too, Yes, I found the solution, I just updated with

before_action :check_subdomain

def check_subdomain
  unless company_signed_in?
    if request.subdomain.present? && params[:controller] == "companies/registrations" && params[:action] == "new"
      redirect_to root_url, subdomain: false
    end
  end
end

write this in application controller. this will work.

before_action :check_subdomain

def check_subdomain
  unless company_signed_in?
    if request.subdomain.present? && params[:controller] == "companies/registrations" && params[:action] == "new"
      redirect_to root_url, subdomain: false
   end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM