LoginSignup
12
5

More than 5 years have passed since last update.

Github pages で 別サイトへリダイレクトさせる

Posted at

概要

  • Github Pagesで静的サイトを作成していたが、別サイトに移行したので、そちらにリダイレクトをさせたかった。
  • Meta タグでやるのはちょっと。
  • Jekyllでできるっぽい → やってみた

参考サイト

手順

Jekyllの導入

Quick-start を参考に、Jekyllでサイトをローカルに構築する

$ gem install jekyll
$ jekyll new myblog
$ cd myblog

JekyllRedirectFrom Plugin を導入

How to redirect URLs on Jekyll site hosted on GitHub Pages を参考に、jekyll-redirect-from を導入する。

GemFile に 下記の1文を追加:

gem 'jekyll-redirect-from'

下記を実行:

$ bundle

_config.yml に下記を追記:

plugins:
  - jekyll-redirect-from

レイアウトを作成

_layouts フォルダに redirected.html ファイルを作成し、下記のコードを記述する。

<!DOCTYPE html>
<html>
<head>
<link rel="canonical" href="{{ page.redirect_to }}"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url={{ page.redirect_to }}" />
</head>
<body>
    <h1>Redirecting...</h1>
      <a href="{{ page.redirect_to }}">Click here if you are not redirected.<a>
      <script>location='{{ page.redirect_to }}'</script>
</body>
</html>

index.mdを編集

トップページからリダイレクトをかけたいので、 index.md を下記のように編集する。

---
# You don't need to edit this file, it's empty on purpose.
# Edit theme's home layout instead if you wanna make some changes
# See: https://jekyllrb.com/docs/themes/#overriding-theme-defaults
layout: redirected
sitemap: false
permalink: /
redirect_to:  {redirect url}
---

コード

動作状況

https://okhiroyuki.github.io/ で確認できます。

12
5
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
12
5