英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论