简体   繁体   中英

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 . 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.

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."

I did check on developer tools and saw that when you click on the "about" button request header is like this

: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

: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?

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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