Nextjs 静态导出重定向问题

Nextjs static export redirecting issue

我在 Next.js 应用程序中遇到重定向问题。

我做了静态导出(next build && next export)。

应用程序的主机位于子目录中(此处为 https://www.myhost.com/subdirectory/**[index.html**),因此我在 next.config.js 中使用了 basePath: '/subdirectory'。 乍一看一切正常。

在应用内重定向工作正常。但是当用户刷新另一个页面上的页面然后索引时 - 假设该页面被命名为 subpagehttps://www.myhost.com/subdirectory/subpage) the site doesn't redirect the user to subpage but rather goes to https://www.myhost.com/(甚至不是项目所在的子目录)。

非常感谢有人的帮助。

这里是 .htaccess 文件。

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html/ [L]

</IfModule>

如果你需要任何其他文件,我可以编辑这个 post 如果你评论它。

提前致谢!!

我假设您的 .htaccess 文件位于 /subdirectory/.htaccess。这些指令的编写就好像应用程序位于文档根目录中,而不是子目录中。

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html/ [L]

</IfModule>

最后的 RewriteRule 指令将所有请求发送到文档根目录。您需要:

  1. 删除 替换 字符串 (/index.html/)
  2. 上的斜杠前缀
  3. 并删除结尾的斜杠。 (通常这会导致 404,除非启用路径信息?)
  4. 完全删除 RewriteBase 指令。
  5. 不需要 <IfModule> 包装器。

考虑到以上几点,尝试以下方法:

# /subdirectory/.htaccess

RewriteEngine On

RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.html [L]