1

I want to redirect all incoming requests to my root domain, and found the code below at devcenter.heroku.com/articles/custom-domains.

This redirect works fine, except that . For example, http://judge.me/faq works but http://www.judge.me/faq redirects to judge.me homepage.

I know I have to add a parameter to this function, but can not find the name of the params key which stores the last section of the URL. Can someone help me or refer me to the right place?

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :ensure_domain

  APP_DOMAIN = 'judge.me'

  def ensure_domain
    if request.env['HTTP_HOST'] != APP_DOMAIN
      # HTTP 301 is a "permanent" redirect
      redirect_to "http://#{APP_DOMAIN}", :status => 301
    end
  end

end
Peter-Jan Celis
  • 169
  • 3
  • 11

1 Answers1

1

That is because you are redirecting the entire domain to another domain. If you want to preserve the url, you need something like

  redirect_to "http://#{APP_DOMAIN}/#{request.path}", :status => 301

I think it is request.url. You can check this thread as well How do I get the current absolute URL in Ruby on Rails?

Community
  • 1
  • 1
unamashana
  • 635
  • 1
  • 5
  • 14