1
1

More than 3 years have passed since last update.

WordPressの引っ越し(移行)で 404 not foundになった時の対処法

Last updated at Posted at 2021-05-18

WordPress(ワードプレス)の引っ越しで、トップページは表示されるもののその他ページが404エラーになってしまうというのはよくある事例。

パーマリンク設定の更新とかですんなり解決すれば良いですが、今回どハマりしたポイントがあったので、対処法についてまとめたいと思います。

「WordPressの引っ越しで404エラーが発生してしまった!!」という方の参考になれば幸いです。

WebサーバーがApacheの場合

WordPressの場合は大抵Apache(アパッチ)を使用していると思います。
Apacheを利用していて404エラーが起きた場合、.htaccessファイルの記述を有効にするというのが解決策です。
対処法①〜③に分かれていますが、この方針は変わりません。

対処法①:パーマリンク設定を更新する
対処法②:.htaccessファイルの記述を確認
対処法③:パーマリンク設定を更新する

それでは順番に解説していきます。

対処法①:パーマリンク設定を更新する

これは404エラーが発生するほとんどのケースで解消することが多い対処法なので、404エラーが発生したら最初に試してみてください。

まず、WordPressの管理画面の「設定」から「パーマリンク設定」を開きましょう。
パーマリンク設定のページが開いたら、何も変更せずに「変更を保存」をクリックします。
(何も変更せずに がポイントです。)

この操作を行うことによって「.htaccessファイル」が上書きされ、404エラーが解決される可能性が高いです。

対処法②:.htaccessファイルの記述を確認

パーマリンク設定を更新しても404エラーが解消しない場合、ルートディレクトリにある「.htaccessファイル」の記述を確認し、必要があれば修正しましょう。

WordPressを引っ越す際にディレクトリの構成を変更したり、「.htaccessファイル」のコピーを忘れていたり、何らかの理由で下記の記述がない場合は、記述を行えば404エラーが解消する可能性があります。

.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^index\.rdf$ /feed/rdf/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

対処法③:パーマリンク設定を更新する

それでも404エラーが解消されない場合は、Apache(アパッチ)のmod_rewriteモジュールに不具合がないか確認をしてください。

「.htaccessファイル」には<IfModule mod_rewrite.c></IfModule>という記述があり、mod_rewriteモジュールに不具合があれば中身が正常に読み込まれないからです。

下記のようなmod_rewriteモジュールがインストールされているか確認をしてください。

# grep mod_rewrite /etc/httpd/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so
# find / -name mod_rewrite.so -print
/usr/lib64/httpd/modules/mod_rewrite.so


WebサーバーがNginxの場合

Nginx(エンジンエックス)を利用してるサーバーにWordPressを移行した場合、デフォルトではパーマリンクの設定が基本以外にすると404エラーが起きます。

これを解消するには「/etc/nginx/nginx.conf」にある設定を変更する必要があります。

nginx.conf
server {
    server_name wordpress-replace.testtotest.com; # managed by Certbot
        root         /usr/share/nginx/html/wordpress;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/wordpress-replace.testtotest.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/wordpress-replace.testtotest.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

この設定ファイルにあるlocation/{}の部分に以下の内容を挿入してください。

server {
    server_name wordpress-replace.testtotest.com; # managed by Certbot
        root         /usr/share/nginx/html/wordpress;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        try_files $uri $uri/ /index.php?$args; //ここを追記する
        }

まとめ:サーバー環境を把握して、解決策を考えよう

自分はWordPressを移行した環境がApacheだと思いこんでいたので、「.htaccessファイル」の変更を軸に検討してしまい、数日時間を溶かしました。笑

結局Nginxだったと分かればすぐに解決できたので、「なぜか404エラーが解決できない」という方は前提条件から疑ってみるのも手かもしれません。

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