【问题标题】:Nextjs static export redirecting issueNextjs 静态导出重定向问题
【发布时间】:2021-11-26 13:14:41
【问题描述】:

我的 Next.js 应用程序出现重定向问题。

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

应用程序的主机位于子目录中(https://www.myhost.com/subdirectory/**[index.html 这里]**)所以我在next.config.js 中使用了basePath: '/subdirectory'。 一切都一见钟情。

在应用程序内重定向工作正常。但是当用户刷新另一个页面上的页面然后索引时 - 假设页面名为 subpage (https://www.myhost.com/subdirectory/subpage) 该站点不会将用户重定向到子页面而是转到 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>

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

提前致谢!!

【问题讨论】:

    标签: javascript .htaccess redirect next.js


    【解决方案1】:

    我假设您的 .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. 并删除尾部斜杠。 (通常这会导致 404,除非启用了路径信息?)
    3. 完全删除RewriteBase 指令。
    4. &lt;IfModule&gt; 包装不是必需的。

    考虑到以上几点,请尝试以下操作:

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

    【讨论】:

    • 你是救生员!谢谢!它现在可以工作了:) 是的,.htacces 是一个错字,我现在编辑了这个问题:)
    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 2022-01-26
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2023-03-27
    • 2021-09-08
    相关资源
    最近更新 更多