Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

`Redirect from naked domain to www from inside the routes doesn't change the domain in the address bar - because of Turbolinks?

Rails • Asked by Vito Botta

Hi, I am trying to force a redirect from naked domain to www from inside the Rails app, since I can't do this with the Nginx ingress in Kubernetes (basically, users can add custom domains and the app itself does some redirect depending on the domain etc, so long story short I cannot force the same redirect for all the requests using Nginx).

I am using a constraint to my routes.rb that checks if the request is for the naked domain, and if that's the case it redirect to the same path but with the www subdomain.

The constraint:

class Constraints::NakedDomain
  def self.matches?(request)
puts request.subdomain
    ( == Settings.domain) && (request.subdomain == "")

In routes.rb:

  constraints(Constraints::NakedDomain) do
        get ':any', to: redirect(subdomain: 'www', path: '/%{any}'), any: /.*/

I can see the www domain in the Network tab of the Dev tools in Chrome, but the domain in the address bar of the browser remains the naked domain. Is it because of Turbolinks? Does anyone know how to fix or anyway how to force redirection to www from inside Rails instead of the web server? Thanks!

Uhm I just noticed using that puts command that the subdomain appears as "www" already so the redirection doesn't happen. But I am definitely visiting the website with the naked domain ?!

Nevermind. The redirection does work and the confusion came from the fact that Chrome/Brave/Safari hide the "www" part of the address. Not sure when this change was made. Firefox still shows the "www".

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,152+ 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.