为什么在我的部署在AWS Amplify上的Astro网站上,死链接会重定向到首页?

huangapple go评论50阅读模式
英文:

Why are dead links redirecting to the home page on my Astro site deployed on AWS Amplify?

问题

我有一个静态的Astro网站部署到AWS Amplify进行托管。我最初错过了一个失效链接,因为它链接到主页而不是专用的404.astro页面。

如何使我的404页面在出现失效/无效的URL时显示,而不是显示主页?

英文:

I have a static Astro website deployed to AWS Amplify for hosting. I initially missed a dead link because it was linking to the home page instead of the dedicated 404.astro page.

How can I get my 404 page to show for dead/invalid URL instead of the home page?

答案1

得分: 0

默认情况下,AWS Amplify在您的应用中设置了一个重定向规则,将重定向到根目录/index.html页面。这是因为单页应用程序(SPA)通常有一个单一的根index.html文件,用于托管前端框架。这个重定向是必要的,以允许深度链接到SPA,因为SPA会进行自己的内部路由。

由于Astro是一个静态站点生成器(SSG),它创建了一个目录结构,并在每个目录中放置一个单独的index.html。它还有一个专用的404.html

这意味着我们需要调整重定向规则:

  • 打开AWS控制台
  • 打开AWS Amplify服务
  • 打开您的Amplify应用
  • 在“应用设置”侧边栏中点击重写和重定向

将重写规则从这个更改为这个:

Source Address   Target address   Type
/<*>             /index.thml      404 (rewrite)

改为这个:

Source Address   Target address   Type
/<*>             /404.thml        404 (rewrite)
英文:

By default, AWS Amplify sets up a redirect rule in your app that redirects to the root /index.html page. This is done because single page applications (SPA) often have a single root index.html file that hosts the frontend framework. The redirect is necessary to allow deep linking into the SPA which does its own internal routing.

Since Astro is a static site generator (SSG) it creates a directory structure and puts a separate index.html inside each each directory. It also has a dedicated 404.html that it generates.

This means we need to adjust the redirect rule:

  • Open the AWS Console
  • Open the AWS Amplify service
  • Open your Amplify app
  • Click Rewrites and redirects in the "App Settings" sidebar

Change the rewrite rule from this

Source Address   Target address   Type
/<*>             /index.thml      404 (rewrite)

To this

Source Address   Target address   Type
/<*>             /404.thml        404 (rewrite)

huangapple
  • 本文由 发表于 2023年6月9日 06:11:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定