I have deployed a small static angular website on GitHub Link but it has one problem. When i refresh any products page like this then I get 404 error.
我在GitHub Link上部署了一个静态的小角度网站,但它有一个问题。当我像这样刷新任何产品页面时,我会收到404错误。
However when i navigate to any products pages from the home page then the product pages opens correctly.
但是,当我从主页导航到任何产品页面时,产品页面都会正确打开。
This app's reprository link is - here.
这个应用程序的转载链接是-这里。
I am beginner in angular - please help me why it is doing so ?
我是角度初学者--请告诉我为什么会这样?
更多回答
This is because you are using HTML pushState as location strategy due to which when you refresh from browser, the browser sends request of your routed page to Github. As Github is unaware of this internal routing by angular due to it shows 404 error.
这是因为您使用的是HTMLPush State作为位置策略,因为当您从浏览器刷新时,浏览器会将您路由的页面的请求发送到Github。因为Github没有意识到这种按角度进行的内部布线,因为它显示了404错误。
There are couple of solutions:
1. If you want to stick to Github then you need to move to HashBased location strategy. It will simply solve your problem.
2. If you don't have any dependency on Github then you can move to other servers (firebase or apache) which support creating a htaccess file or any other fallback mechanism. (See this link for more info). This htaccess file is unfortunately not supported by Github yet. I personally prefer firebase due to these glitches.
有几个解决方案:1.如果你想坚持使用Github,那么你需要转向基于HashBased的定位策略。它会简单地解决你的问题。2.如果你对Github没有任何依赖,那么你可以转移到其他支持创建htaccess文件或任何其他后备机制的服务器(Firebase或APACHE)。(有关更多信息,请参阅此链接)。遗憾的是,Github还不支持此htaccess文件。由于这些故障,我个人更喜欢火力基地。
By these fallback mechanisms I mean the server redirect to index.html i.e. your main HTML file in case of any 404 routes.
通过这些后备机制,我的意思是服务器重定向到index.html,即对于任何404路由,您的主HTML文件。
I had the same issue. Either you use the locationHashStrategy you find here: https://angular.io/guide/router#appendix-locationstrategy-and-browser-url-styles
我也有同样的问题。要么使用您在此处找到的LocationHashStrategy:https://angular.io/guide/router#appendix-locationstrategy-and-browser-url-styles
But then your URLs will have an additional # like: http://yourdomain.com/#/products
但是,您的URL将有一个额外的#Like:http://yourdomain.com/#/products
If you don't want that you have to add .htaccess and rewrite everything to index.html.
如果您不想这样做,则必须添加.htaccess并将所有内容重写为index.html。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Save this as .htaccess in the same folder where your index.html is.
在您的index.html所在的同一文件夹中将其另存为.htaccess。
you can create a html file like below (src/404.html)
您可以创建如下所示的html文件(src/404.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0;url=app_path">
<title>Redirecting...</title>
</head>
<body>
<p>If you are not redirected, <a href="app_path">click here</a>.</p>
</body>
</html>
and in appRoutingModule use this - imports: [RouterModule.forRoot(routes, { useHash: false })],
并且在appRoutingModule Use This-Imports:[RouterModule.forRoot(routes,{useHash:False})]中,
then run a build command and check for 404 file inside you docs or root.
For me the above one is working
然后运行构建命令并检查您的文档或根目录中是否有404文件。对我来说,上面的方法是有效的
更多回答
thank you ankit I now know the problem cause. I think this will continue to not-work on github. So I will move to firebase.
谢谢你,安吉,我现在知道问题的原因了。我认为这将继续不适用于GitHub。所以我要搬到火力基地去。
Where in my answer is a link to github?
在我的答案中,哪里有GitHub的链接?
thank you @CarstenEnnulat for the explanation. Unfortunately github will not allow htaccess file.
感谢您@Carsten Ennulat的解释。遗憾的是,GitHub不允许htaccess文件。
Because you are telling them to add .htaccess file whereas github doesn't allow such file support.
因为您告诉他们添加.htaccess文件,而GitHub不允许这样的文件支持。
@yogihosting okay, didn't know that, in that case you have to switch to hash locationstrategy
@yogihost好的,我不知道,在这种情况下,你必须切换到散列位置策略
我是一名优秀的程序员,十分优秀!