2026年8月25日
2026年8月25日
WordPressのトップページが404エラーになる問題を解決する方法
はじめに
WordPressのトップページにアクセスすると404エラーが表示される・「読書中のページが見つかりません」と表示されてサイトのホームページが表示されない・管理画面の設定→表示設定でフロントページを固定ページに変更したら表示されなくなった・サーバー移転後にトップページだけ404になるといった問題は、パーマリンク設定のリセット・.htaccessのRewriteBase設定ミス・フロントページ設定の不整合が原因です。
症状・原因
- パーマリンク設定を変更した後に
.htaccessの書き換えが正しく行われていない - 設定→表示設定で指定した固定ページが削除されているかゴミ箱に入っている
- サブディレクトリインストールで
RewriteBaseが正しく設定されていない WP_HOMEとWP_SITEURLの不一致でホームURLが404になっている
解決手順
ステップ1:404の原因を診断する
# WP-CLIでフロントページ設定を確認
wp eval "
echo '=== Front Page Settings ===' . PHP_EOL;
echo 'show_on_front: ' . get_option('show_on_front') . PHP_EOL;
echo 'page_on_front: ' . get_option('page_on_front') . PHP_EOL;
echo 'page_for_posts: ' . get_option('page_for_posts') . PHP_EOL;
// フロントページとして設定されているページの確認
\$front_id = (int) get_option('page_on_front');
if (\$front_id) {
\$page = get_post(\$front_id);
echo 'Front page title: ' . (\$page ? \$page->post_title : 'NOT FOUND') . PHP_EOL;
echo 'Front page status: ' . (\$page ? \$page->post_status : 'N/A') . PHP_EOL;
}
echo '=== URL Settings ===' . PHP_EOL;
echo 'siteurl: ' . get_option('siteurl') . PHP_EOL;
echo 'home: ' . get_option('home') . PHP_EOL;
"
# .htaccessの内容を確認
wp eval "
\$htaccess = ABSPATH . '.htaccess';
if (file_exists(\$htaccess)) {
echo file_get_contents(\$htaccess) . PHP_EOL;
} else {
echo '.htaccess not found' . PHP_EOL;
}
"
# rewriteルールを確認
wp eval "
global \$wp_rewrite;
\$wp_rewrite->flush_rules();
\$rules = get_option('rewrite_rules');
echo 'Total rewrite rules: ' . count(\$rules) . PHP_EOL;
echo 'Front page rule exists: ' . (isset(\$rules['^$']) ? 'yes' : 'no') . PHP_EOL;
"
ステップ2:パーマリンクとrewriteルールを修正する
# パーマリンクをリセット(最も効果的な修正方法)
wp rewrite flush --hard
# パーマリンク設定をデフォルトに戻してから再設定
wp rewrite structure '/%postname%/' --hard
# .htaccessを再生成
wp eval "
\$htaccess_file = ABSPATH . '.htaccess';
\$rules = get_option('rewrite_rules');
if (insert_with_markers(\$htaccess_file, 'WordPress', explode(\"\\n\", \$GLOBALS['wp_rewrite']->mod_rewrite_rules()))) {
echo '.htaccess updated successfully' . PHP_EOL;
} else {
echo 'Failed to update .htaccess - check file permissions' . PHP_EOL;
}
"
# .htaccessのパーミッションを修正
wp eval "
\$htaccess = ABSPATH . '.htaccess';
echo 'Current permissions: ' . decoct(fileperms(\$htaccess) & 0777) . PHP_EOL;
"
# .htaccess: WordPress標準のrewriteルール(ルートインストール)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# .htaccess: サブディレクトリインストールの場合(例:/wordpress/)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
ステップ3:フロントページ設定を修正する
# フロントページの設定をリセット(最新の投稿を表示)
wp option update show_on_front 'posts'
# または特定の固定ページをフロントページに設定
wp eval "
// 「ホーム」というタイトルの固定ページを検索
\$pages = get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'title' => 'ホーム',
'numberposts' => 1,
]);
if (\$pages) {
update_option('show_on_front', 'page');
update_option('page_on_front', \$pages[0]->ID);
echo 'Front page set to: ' . \$pages[0]->post_title . ' (ID: ' . \$pages[0]->ID . ')' . PHP_EOL;
}
"
# ゴミ箱の固定ページを復元
wp eval "
\$trashed_pages = get_posts([
'post_type' => 'page',
'post_status' => 'trash',
'numberposts' => -1,
]);
foreach (\$trashed_pages as \$page) {
echo 'Trashed page: ' . \$page->post_title . ' (ID: ' . \$page->ID . ')' . PHP_EOL;
}
"
# IDを指定してページを復元
wp post update 42 --post_status=publish
// functions.php: フロントページを強制的に設定
add_action('init', function(): void {
if (get_option('show_on_front') !== 'page') {
return;
}
$front_id = (int) get_option('page_on_front');
if (!$front_id || get_post_status($front_id) !== 'publish') {
// フロントページが見つからない場合は最新投稿に切り替え
update_option('show_on_front', 'posts');
update_option('page_on_front', 0);
}
});
ステップ4:WP_HOMEとWP_SITEURLの設定を修正する
// wp-config.php: URLを正しく設定
define('WP_HOME', 'https://yourdomain.com');
define('WP_SITEURL', 'https://yourdomain.com');
// サブディレクトリにWordPressをインストールしてルートで公開する場合
// define('WP_HOME', 'https://yourdomain.com'); // 公開URL
// define('WP_SITEURL', 'https://yourdomain.com/wordpress'); // WPファイルの場所
# データベースのURL設定を確認・修正
wp eval "
echo 'DB siteurl: ' . get_option('siteurl') . PHP_EOL;
echo 'DB home: ' . get_option('home') . PHP_EOL;
"
# URLを修正
wp option update siteurl 'https://yourdomain.com'
wp option update home 'https://yourdomain.com'
# キャッシュをクリア
wp cache flush
wp rewrite flush --hard
ステップ5:サーバー設定とmod_rewriteを確認する
# mod_rewriteが有効か確認
wp eval "
if (function_exists('apache_get_modules')) {
\$modules = apache_get_modules();
echo 'mod_rewrite: ' . (in_array('mod_rewrite', \$modules) ? 'enabled' : 'DISABLED') . PHP_EOL;
} else {
echo 'apache_get_modules not available (may be Nginx)' . PHP_EOL;
}
"
# Nginxの場合のWordPress設定確認
wp eval "
echo 'Server software: ' . (\$_SERVER['SERVER_SOFTWARE'] ?? 'unknown') . PHP_EOL;
"
# Nginx: WordPress のrewriteルール
server {
listen 80;
server_name yourdomain.com;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
// functions.php: トップページが404の場合の診断情報を出力(デバッグ用)
add_action('wp', function(): void {
if (!is_404() || !current_user_can('manage_options')) {
return;
}
error_log('[404 Debug] Request URI: ' . $_SERVER['REQUEST_URI']);
error_log('[404 Debug] show_on_front: ' . get_option('show_on_front'));
error_log('[404 Debug] page_on_front: ' . get_option('page_on_front'));
error_log('[404 Debug] queried_object: ' . print_r(get_queried_object(), true));
});
注意事項
- パーマリンク設定を変更する前に必ず
.htaccessファイルのバックアップを取ってください。サーバーによっては.htaccessへの書き込み権限がなく、手動で更新が必要です - Nginxを使用している場合、
.htaccessファイルは機能しません。Nginxの設定ファイルにtry_files $uri $uri/ /index.php?$args;が設定されているか確認してください AllowOverride AllがApacheの設定に含まれていないと.htaccessのRewriteルールが機能しません。ホスティング会社に確認してください
まとめ
WordPressトップページ404エラーの解決は①show_on_front・page_on_front設定確認・フロントページの固定ページがpublish状態か確認・.htaccessのRewriteルール確認、②wp rewrite flush --hardでパーマリンクとrewriteルールをリセット・.htaccessを手動で標準のWordPressルールに書き換え・RewriteBaseをインストールパスに合わせて設定、③ゴミ箱に入った固定ページをpublishに戻す・show_on_frontをpostsに変更してから再設定・page_on_frontに有効な固定ページIDを設定、④wp-config.phpのWP_HOME・WP_SITEURLをサイトURLに統一・DBのhome・siteurlオプションを修正・wp cache flushでキャッシュをクリア、⑤Apacheのmod_rewrite有効確認・AllowOverride Allの設定確認・Nginxはtry_filesディレクティブの確認の手順で解決します。