0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Herokuのサブドメインから独自ドメインへお引越し(リダイレクト) nginx編

0
Last updated at Posted at 2021-02-03

はじめに

こんにちは。HerokuにWordPress入れてブログ始めた筆者です。

初めて数カ月、GoogleAdSenseの審査に落ちまくり、先駆者の奮闘ブログを拝読しまして、やはり独自ドメインにする必要があるのではと感じました。

今までは、herokuのサブドメイン(example.herokuapp.com)で運用していました。

そんなわけでHerokuのサブドメインから独自ドメインへのリダイレクトの設定を行ったのですが、Herokuならでは(?)の問題にぶち当たったので情報の共有のため記事にしました。

ぶつかった問題

1. おいおい、Herokuのカスタムのnginx.confでserverブロック使えないんだが!!

Googleでnginx ドメイン リダイレクトとかで検索してでてくるこんなやつ↓ 使えば簡単に引っ越せると思ってましたが、なんと、Herokuのカスタムのnginx.confではserverブロックが使えません... :sob:

server {
    server_name old-domain.com;
    rewrite ^(.*)$ https://new-domain.com$1 permanent;
}

ググってもググってもserverブロックでリダイレクトする記述ばかり、滅多にnginx.confいじらない筆者にとっては、手厳しい...。

解決策

これならHerokuのカスタムのnginx.confでも行けます:ok_hand:

if ($host = "old-domain.com") {
    rewrite ^ https://new-domain.com$request_uri? permanent;
}

なんだ、if文とか使えるんだ、勉強になりました。

おわりに

無事にお引越しができました。私と同じようにHerokuでnginxのリダイレクトでお困りの方の救いになれば幸いです。
尚、GoogleAdSenseの審査はまだ通っていません :pray:

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?