Is there a configuration in Github Pages that allows you to redirect everything to index.html for a Single Page App?

末鹿安然 提交于 2019-11-28 08:42:07

问题


I'm trying to post my SPA app that works fine locally but when I push it to Github Pages, the interior pages don't register if you navigate to them directly.

For example http://USER.github.io/PROJECT_NAME/ works but http://USER.github.io/PROJECT_NAME/about doesn't because theres no redirect or rewrite. The index.html is located at the root of the project.


回答1:


I just built this tiny package (for bower / npm) to solves that exact problem so I thought I'd share it here as an answer to your question,

https://github.com/websemantics/gh-pages-spa

If you include the package in your 404.html and index.html pages, it will redirect all the traffic to Index.html,

It supports Project and User/Org Pages type repositories, handles QueryStrings and comes with a working example,




回答2:


Github pages allows you to create a 404.html page that will be shown each time ... there is a 404 error. If http://USER.github.io/PROJECT_NAME/about doesn't exists, it will show your 404.html content with the "not found" url as window.location.

So, this page can contain a script that redirect to a hashbang style route. eg : react router, even using clean urls (browserHistory) can understand a route like PROJECT_NAME/#/about and will automatically push to PROJECT_NAME/about.

That's ugly !




回答3:


Since you're using React, the HOWTO in create-react-app should work.

Basically, add a homepage field to package.json and install the gh-pages package.



来源:https://stackoverflow.com/questions/36296012/is-there-a-configuration-in-github-pages-that-allows-you-to-redirect-everything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!