从旧域重定向到新域(SEO友好)

我将Heroku应用程序上的自定义域更改为新域。 现在我将创建一个新的Heroku应用程序,其唯一目的是重定向到第一个应用程序。

我在Google网站管理员中读到,我应该像这样执行301重定向:

http://old.com/anypath/123 to http://new.com/anypath/123 

我如何在Rails中执行此操作?

将它放在ApplicationControlller中的beforefilter中:

 class ApplicationController before_action :redirect_if_old protected def redirect_if_old if request.host == 'old.com' redirect_to "#{request.protocol}new.com#{request.fullpath}", :status => :moved_permanently end end end 

在您的控制器操作中:

 redirect_to "http://new.com#{request.request_uri}", :status => 301 

但是,对于你在开发中心记录的内容,Heroku有一个更好的选择 :

 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}#{request.request_uri}", :status => 301 end end end