Next.js 索引路线 returns 404

Next.js index route returns 404

我在生产服务器上获取 nextjs 应用程序 运行 时遇到问题。 我通过以下代码通过 htaccess 转发它:

重写规则 ^(.*) <a href="http://127.0.0.1:1338/" rel="nofollow noreferrer">http://127.0.0.1:1338/</a>$1

每条路线都运行良好:/blog、/blog/:id、/about 等
从服务器端呈现时,只是“/”不起作用。我可以在加载另一条路线后毫无问题地导航到它。 我也可以用 /index 调用它,但不能用 mydomain.tld

有人知道吗?

您正在尝试使用 Apache 模块进行重定向,但您似乎正在使用 Node.js

试着看看 nodejs equivalent of this .htaccess

当 Apache 试图将 index.html 文件传递​​到下一个 returns 错误的路由器时,就会出现此问题。

DirectoryIndex disabled 添加到 .htaccess 文件可以解决问题。

这里是一个完整的 .htaccess,用于实现带有 Apache 重定向的 Node.js 应用程序。

RewriteEngine On

# Need to disable DirectoryIndex to avoid rewriting directory URL to index.html
DirectoryIndex disabled

# Redirect home page request to the NextJS server
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://127.0.0.1:4002/ [P,L]

# Redirect all other requests to the NextJS server with the URI appended
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:4002/ [P,L]