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?

WordPressに独自のHTMLを使用してメンテナンス画面を表示

Posted at

WordPressにメンテナンス画面を設置するには、プラグインを使用する方法と手動で設定する方法の2つがあります。
独自のHTMLを使用してメンテナンス画面を表示するためには、手動で設定する方法が適しています。以下にその手順を説明します。

手動でメンテナンスモードを設定する

手順1 メンテナンス用のHTMLファイルを作成する

まず、メンテナンス画面として表示したいHTMLファイルを作成します。
例えば、以下のような内容です

<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>メンテナンス中</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f2f2f2;
            font-family: Arial, sans-serif;
        }
        .container {
            text-align: center;
        }
        h1 {
            font-size: 2em;
            margin-bottom: 0.5em;
        }
        p {
            font-size: 1.2em;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>メンテナンス中です</h1>
        <p>現在、メンテナンス作業を行っています。しばらくお待ちください。</p>
    </div>
</body>
</html>

このファイルをmaintenance.htmlとして保存します。

手順2 maintenance.htmlをサーバーにアップロードする

作成したmaintenance.htmlファイルをWordPressのルートディレクトリ(wp-contentフォルダと同じ階層)にアップロードします。

手順3 maintenance.phpファイルを作成する

次に、maintenance.phpという名前のファイルを作成し、以下のコードを記述します

// IPアドレスを指定して、特定のIPアドレスからのアクセスを許可する
$allowed_ips = ['123.456.789.000']; // ここに許可するIPアドレスを追加

if (!in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)) {
    // メンテナンス中のHTMLファイルのパス
    $maintenance_file = dirname(__FILE__) . '/maintenance.html';
    // メンテナンス画面を表示
    if (file_exists($maintenance_file)) {
        include($maintenance_file);
        exit;
    }
}
?>

このファイルをWordPressのルートディレクトリにアップロードします。

手順4 .htaccessファイルを編集する

WordPressのルートディレクトリにある.htaccessファイルを開き、以下のコードを追加します

<IfModule mod_rewrite.c>
    RewriteEngine On

    # メンテナンスモードを有効にする
    RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000$  # ここに許可するIPアドレスを追加
    RewriteCond %{REQUEST_URI} !/maintenance\.html$ 
    RewriteRule ^.*$ /maintenance.php [R=302,L]
</IfModule>

この設定により、指定したIPアドレス以外からのアクセスはmaintenance.phpファイルを経由してmaintenance.htmlが表示されます。

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?