Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku subdomain duplicate content? How to redirect to domain?

Tags:

seo

heroku

Google has indexed my Heroku app subdomain: myapp.heroku.com

Is it duplicate content?

How should I redirect myapp.heroku.com to mydomain.com?

like image 646
Rails beginner Avatar asked Mar 22 '11 21:03

Rails beginner


3 Answers

According to Heroku docs for custom domains, you could do it like so:

class ApplicationController
  before_filter :ensure_domain

  APP_DOMAIN = 'myapp.mydomain.com'

  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

I use this method and it works fine. Note that since the redirect returns a 301 http status (a permanent redirect) your site won't be penalized for duplicate content.

The 301 status is the only point missing in Markus' solution, but I think it is an important one if your concern is with SEO.

Edit: Something that's not on the docs and I forgot to mention - you should exclude the environments you don't want the redirect applied to. You could change the if statement to something like:

if request.env['HTTP_HOST'] != APP_DOMAIN && ENV["RAILS_ENV"] != 'development'
like image 121
rapcal Avatar answered Sep 28 '22 22:09

rapcal


Use the Heroku add-on custom domains:

heroku addons:add custom_domains:basic
heroku domains:add www.myapp.com
heroku domains:add myapp.com

In addition, you have to take some configuration steps at the admin interface of your domain provider. You need a CNAME to proxy.heroku.com and three A-RECORDs to the Heroku IPs. You find this in the Heroku Docs.

Edit to respond to another answer below. You can redirect myapp.com to www.myapp.com in your routes.rb:

 constraints(:host => /^communityguides.eu/) do
    root :to => redirect("http://www.communityguides.eu")
    match '/*path', :to => redirect {|params| "http://www.communityguides.eu/#{params[:path]}"}
  end
like image 29
Markus Proske Avatar answered Sep 28 '22 21:09

Markus Proske


I suggest using rack-canonical-host to redirect Heroku's subdomain to your custom domain.

like image 35
Henrik N Avatar answered Sep 28 '22 21:09

Henrik N