Google has indexed my Heroku app subdomain: myapp.heroku.com
Is it duplicate content?
How should I redirect myapp.heroku.com to mydomain.com?
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'
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
I suggest using rack-canonical-host to redirect Heroku's subdomain to your custom domain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With