[英]Apache httpd refresh
I am working on an angular project with routing and it works fine in local.我正在开发一个带有路由的角度项目,它在本地运行良好。 When I deploy the same in Apache httpd, on browser refresh from any page it gives the error 'The requested URL was not found on this server.'
当我在 Apache httpd 中部署相同的内容时,在从任何页面刷新浏览器时,它会给出错误“在此服务器上找不到请求的 URL”。
The comments of MikeOne and Chad K are right, that is one way of solving this issue. MikeOne 和 Chad K 的评论是对的,这是解决此问题的一种方法。
Firstly, when you build an app for production ng build --prod
sometimes you need to change the base href
in the index.html
if the app is not in the root of your webserver, I think this is not your problem but you could check it anyway.首先,当您为生产
ng build --prod
构建应用程序时,如果应用程序不在您的网络服务器的根目录中,有时您需要更改index.html
的base href
,我认为这不是您的问题,但您可以检查反正它。
After that, let's say your routing is going to ' http://yourserver/page1/ ' when the app is reloaded from that URL the webserver will try to find a directory named page1
which does not exist.之后,假设您的路由将转到“ http://yourserver/page1/ ”,当从该 URL 重新加载应用程序时,网络服务器将尝试查找名为
page1
的目录,但该目录不存在。 The proposed solutions on the comments would work, using apache mod rewrite.建议的评论解决方案将起作用,使用 apache mod 重写。 An that solution would only work if you have access to the configuration files of that server or if it uses
.htaccess
files, which sometimes is not the case.只有当您有权访问该服务器的配置文件或者它使用
.htaccess
文件时,该解决方案才有效,有时情况并非如此。 However, if you don't mind having the URLs a little bit different there is another much simpler solution.但是,如果您不介意 URL 稍有不同,还有另一种更简单的解决方案。
in the import
of the RouterModule
where there is something like:在
RouterModule
的import
中,有如下内容:
RouterModule.forRoot( routes )
You can add the useHash
this way:您可以通过以下方式添加
useHash
:
RouterModule.forRoot( routes, { useHash: true } )
then rebuild your project with the production flag and the URLs now will be like:然后使用生产标志重建您的项目,现在的 URL 将如下所示:
http://yourserver/#/page1/
this way, thanks to the hash, the app will work without any problems and the only thing needed is setting the useHash
on the RouterModule and rebuilding your app.这样,多亏了散列,应用程序将毫无问题地工作,唯一需要的是在
useHash
上设置 useHash 并重建您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.