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?

Vito Botta asked in Rails

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".

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,969+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.