简体   繁体   English

当从浏览器输入 url 时,AWS 托管(s3,cloudfront)static 反应网站不会路由到子页面

[英]AWS hosted (s3, cloudfront) static react website does not route to sub page when the url entered from browser

I have hosted a static react website on aws and use s3 , cloudfront .我在aws上托管了一个 static react 网站并使用s3cloudfront It has several subpages.它有几个子页面。

<Link to='/about'>about us</Link>
<Link to='/projects'>projects</Link>
<Link to='/stats'>statistics</Link>
<Link to='/contact'>contact</Link>

When you entered to home page from browser and select one of these subpage's button it will successfully redirect to sub page.当您从浏览器和 select 这些子页面的按钮之一进入主页时,它将成功重定向到子页面。

but when you entered the url directly ( www.example.com/about ) in browser it wont redirect to the subpage and gives this error ie "This XML file does not appear to have any style information associated with it. The document tree is shown below."但是当您在浏览器中直接输入 url ( www.example.com/about ) 时,它不会重定向到子页面并给出此错误,即“此 XML 文件似乎没有任何与之关联的样式信息。显示了文档树以下。”

I did check on developer tools and saw that when you click on the "about" button request header is like this我确实检查了开发人员工具,发现当您单击“关于”按钮时,请求 header 是这样的

:path: /static/js/6.gc48fd92.chunk.js
referer: https://www.example.com/about

but when you entered the url in browser directly ie ( www.example.com/about ), the request header is like this但是当您直接在浏览器中输入 url 即( www.example.com/about )时,请求 header 是这样的

:path: /about

and "referer" is not there.并且“推荐人”不存在。

Am i missing something here?我在这里错过了什么吗? Is there a way to redirect to static js page when you entered the subpage's url in browser?当您在浏览器中输入子页面的 url 时,有没有办法重定向到 static js 页面?

Thanks in advance.提前致谢。

You need to set up Cloudfront so that when the user directly navigates to the page, CloudFront doesn't send back an error because it can't find a file for that page (this is the XML error you get), but instead passes that request onto / so React can take over.您需要设置 Cloudfront,以便当用户直接导航到该页面时,CloudFront 不会发回错误,因为它找不到该页面的文件(这是您得到的 XML 错误),而是通过请求到/所以 React 可以接管。

https://gist.github.com/bradwestfall/b5b0e450015dbc9b4e56e5f398df48ff#spa https://gist.github.com/bradwestfall/b5b0e450015dbc9b4e56e5f398df48ff#spad

The gist of this is you tell CloudFront that on any 404 error, redirect to the root page and return a 200 to the user.这样做的要点是您告诉 CloudFront 在任何 404 错误时,重定向到根页面并向用户返回 200。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM