Skip to main content

Change Layout based on current URL

Rails • Asked by Sascha M.
61678879e67a9ea287dbe443b44d84c9

Hi all,

let's say I wand to build a whitelabel product.

I want to display different layouts (Logos, CSS, ...) based on the current URL the website was accessed by.

E.G.
a123.mywebsite.com should render a different layout than
b124.mywebsite.com

I couldn't find anything describing something like that anywhere :(

Thanks in advance!


Ce795239ba5dd2384fc2f88ffaff5451

As a really trivial example, you can do what they mentioned on StackOverflow here: https://stackoverflow.com/questions/6748168/changing-rails-3-apps-layout-based-on-the-hostname-domain-for-branding

  layout :domain_layout

    def domain_layout
    controller.request.host
    end

Which would look for app/views/layouts/a123.mywebsite.com.html.erb, etc.

You can do other tweaks to that to decide which file to render.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 18,000+ developers who get early access to new screencasts, articles, guides, updates, and more.

By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

More of a social being? We're also on Twitter and YouTube.