LoginSignup
19
16

More than 5 years have passed since last update.

[SEO対策] Railsでherokuapp.comを正しいFQDNへリダイレクトさせる

Last updated at Posted at 2014-10-22

ブログより転機: http://konyu.hatenablog.com/entry/2014/10/22/165553

同一ドメインで
herokuをプラットフォームで利用している場合に、
ドメインを取得してアプリを使用しても、デフォルトのドメインである

xxx.herokuapp.comからでもアクセスできてしまう。

SEO的にxxx.herokuapp.comでインデクシングされてしまうのはよろしくない(らしい)

なのでxxx.herokuapp.comでアクセスされたら301でリダイレクトさせてあげる

つまりアクセスしてほしいドメインが

www.example.com

だとすると

以下のように、ロボットにアクセスされた場合

http://xxx.herokuapp.com/users/100

このようにFQDNより下は同じにしてリダイレクトするようにする

http://www.example.com/users/100

修正ファイル

application_controller.rbで、ensure_domainを定義してやりbefore_filterで常に呼んであげる

before_filter :ensure_domain


# redirect correct server from herokuapp domain for SEO
def ensure_domain
 return unless /\.herokuapp.com/ =~ request.host

 # 主にlocalテスト用の対策80と443以外でアクセスされた場合ポート番号をURLに含める 
 port = ":#{request.port}" unless [80, 443].include?(request.port)
 redirect_to "#{request.protocol}#{FQDN}#{port}#{request.path}", status: :moved_permanently
end

リダイレクト先の構成要素のメモ

  • request.protocol => "https://" や "http://"
  • request.port => 80, 443とかlocalだと3000
  • request.path => "/users/100"
  • FQDNはリダイレクトさせたい固定値
19
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
16