レンタルサーバーでNext.js(SSG)とWordPress(REST API)動かす為の前手順として
.htaccessの記述が必要になると思うのだけど、今回はその記述を書いていきます.
この記述はSEO的にはあまり宜しくないかもです、あくまでも一例です.
Next.js側は.htaccessからブログのフロント側を作ってなどとAIエージェントに投げれば生成してくれると思います.その後、手直し等は必要だと思います.
尚、下記の記述はまだ不完全な部分があると思うのでご指摘を待っています.
.htaccess
# ==================================================
# Rewrite Engine
# ==================================================
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# ==================================================
# 除外ディレクトリ (Rewriteしない)
# ==================================================
RewriteRule ^(dir1|dir2)/ - [L]
# ==================================================
# 1 WordPress REST API (Next.js用)
# ==================================================
RewriteRule ^wp-json/?(.*)$ /index.php [L,QSA]
# ==================================================
# 2 Feed (SEO / RSS)
# ==================================================
RewriteRule ^feed/?$ /index.php?feed=rss2 [L,QSA]
RewriteRule ^feed/rss/?$ /index.php?feed=rss2 [L,QSA]
RewriteRule ^feed/atom/?$ /index.php?feed=atom [L,QSA]
RewriteRule ^rss/?$ /index.php?feed=rss2 [L,QSA]
# ==================================================
# 3 Bot判定 (SEO + SNS OGP)
# ==================================================
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|slurp|duckduckbot|yandex|baiduspider) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} (threads|hatena|facebookexternalhit|facebot|twitterbot|linkedinbot|pinterestbot) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} (slackbot|discordbot|telegrambot|whatsapp|line) [NC]
RewriteRule ^ - [E=IS_BOT:1]
# ==================================================
# 4 Next.js static files
# ==================================================
RewriteCond %{DOCUMENT_ROOT}/next-app/out%{REQUEST_URI} -f
RewriteRule ^(.*)$ /next-app/out/$1 [L]
# ==================================================
# 5 Next.js _next assets
# ==================================================
RewriteRule ^_next/(.*)$ /next-app/out/_next/$1 [L]
# ==================================================
# 6 Human → Next.js routing
# ==================================================
RewriteCond %{ENV:IS_BOT} !=1
RewriteRule ^posts/.*$ /next-app/out/posts/template/index.html [L]
RewriteCond %{ENV:IS_BOT} !=1
RewriteRule ^category/.*$ /next-app/out/category/template/index.html [L]
RewriteCond %{ENV:IS_BOT} !=1
RewriteRule ^tag/.*$ /next-app/out/tag/template/index.html [L]
# 検索
RewriteCond %{ENV:IS_BOT} !=1
RewriteCond %{QUERY_STRING} (^|&)s=.* [NC]
RewriteRule ^$ /next-app/out/search/index.html [L]
# 日付アーカイブURL (Next.js)
RewriteCond %{ENV:IS_BOT} !=1
RewriteRule ^[0-9]{4}/?$ /next-app/out/template/index.html [L]
RewriteCond %{ENV:IS_BOT} !=1
RewriteRule ^[0-9]{4}/[0-9]{2}/?$ /next-app/out/template/template/index.html [L]
RewriteCond %{ENV:IS_BOT} !=1
RewriteRule ^[0-9]{4}/[0-9]{2}/[0-9]{2}/?$ /next-app/out/template/template/template/index.html [L]
# 日付URL (個別記事)
RewriteCond %{ENV:IS_BOT} !=1
RewriteRule ^[0-9]{4}/[0-9]{2}/[0-9]{2}/.*\.html.*$ /next-app/out/posts/template/index.html [L]
# ==================================================
# 7 Next.js SPA fallback (Human)
# ==================================================
RewriteCond %{ENV:IS_BOT} !=1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /next-app/out/index.html [L]
# ==================================================
# 8 Bot → WordPress
# ==================================================
RewriteCond %{ENV:IS_BOT} =1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
# ==================================================
# 9 WordPress fallback
# ==================================================
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>