简体   繁体   English

使用URL访问时出现角度错误500

[英]Angular error 500 when accessing with URL

I'm setting up an Angular web application with routing : https://wei.insa-cvl.org 我正在设置带有路由的Angular Web应用程序: https : //wei.insa-cvl.org

When I use the links inside this application, I can open the different pages. 当我使用此应用程序内的链接时,可以打开不同的页面。 For example, clicking on the "Mot de passe oublié ?" 例如,单击“ Mot de passeoublié?” link brings me to the good page : https://wei.insa-cvl.org/auth/forgot-password . 链接将我带到了好的页面: https : //wei.insa-cvl.org/auth/forgot-password However, when I try to access this page through the URL , I get an internal server error. 但是,当我尝试通过URL访问此页面时,出现内部服务器错误。

I think the problem comes from the .htacess file, but even if I remove it, I'm always getting this error. 我认为问题来自.htacess文件,但是即使删除它,也总是会出现此错误。

The .htaccess file ( source : .htaccess文件( 来源

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]

    RewriteRule ^ /index.html
</IfModule>

The routing : 路由:

{path: '', component: ApplicationComponent, canActivate: [AuthGuardService]},
{path: 'auth', component: AuthComponent},
{path: 'auth/forgot-password', component: ForgotPasswordComponent}
...
{path: '**', redirectTo: '/auth', pathMatch: 'full'}

I would like to access the pages through the URL and not only by clicking on the 'in-app links'. 我想通过URL而不是仅通过单击“应用程序内链接”来访问页面。 Is it possible ? 可能吗 ?

EDIT : Hurray I finally found a solution ! 编辑:万岁,我终于找到了解决方案! I had to add {useHash: true} to my app-routing.module.ts and using the hash URL, like this one : https://wei.insa-cvl.org/#/policy But I don't understand, why ?? 我必须将{useHash: true}添加到我的app-routing.module.ts并使用哈希URL,如下所示: https : {useHash: true}但是我不明白,为什么??

Hurray I finally found a solution ! 万岁,我终于找到了解决方案! I had to add {useHash: true} to my app-routing.module.ts and using the hash URL, like this one : https://wei.insa-cvl.org/#/policy But I don't understand, why ?? 我必须将{useHash:true}添加到我的app-routing.module.ts并使用哈希URL,如下所示: https ://wei.insa-cvl.org/#/policy 但是我不明白,为什么??

This is because the hash and what comes after is not sent to the server. 这是因为散列及其后的内容不会发送到服务器。 In your case, it works because Angular place the hash strategically right after the hostname, this way, when the user change the URL manually, the browser will take only the domain name and use it in the request which your webserver will receive, and since no file was specified, will reply with the default file associated, usually the index.html . 在您的情况下,它之所以起作用是因为Angular在主机名之后策略性地将哈希放在了主机名之后,这样,当用户手动更改URL时,浏览器将仅采用域名并将其用于您的Web服务器将收到的请求中,并且未指定文件,将使用关联的默认文件(通常是index.html答复。

Even though this approach is valid and works in most cases, you can deal with this situation differently, like you were trying to. 即使这种方法有效并且在大多数情况下都可以使用,您也可以像尝试那样以不同的方式处理这种情况。 In your webserver, on every 404 request (meaning your webserver don't know the route), you can reply with a 200 and the index.html and let the Angular app deal with the routing internally or if you are using the webserver only for the Angular app, you can always return the index.html file. 在你的服务器上,每个404请求(这意味着你的Web服务器不知道路线),您可以用回复200index.html ,让角应用程序处理的内部路由,或者如果您使用的网络服务器为在Angular应用中,您始终可以返回index.html文件。

I'm assuming you are using Apache as your webserver, check this StackOverflow for more help with that. 我假设您使用Apache作为Web服务器,请查看此StackOverflow以获得更多帮助。

Adding this configuration to the webserver allows you to use Angular Universal which allows you to render your templates from the server making the loading of the page faster. 通过将此配置添加到Web服务器,可以使用Angular Universal ,它可以使您从服务器呈现模板,从而可以更快地加载页面。

When you request the URL 当您请求URL时

https://wei.insa-cvl.org/#/policy

you are actually requesting the following URL: 您实际上是在请求以下URL:

https://wei.insa-cvl.org/index.html#/policy

See the index.html file ? 看到index.html文件了吗?

Since index.html is the file hosting your Angular app, your Angular code is executed and Angular can process the hashtag fragment of the URL: #/policy . 由于index.html是托管Angular应用程序的文件,因此将执行Angular代码,并且Angular可以处理URL的#/policy标签片段: #/policy Everything works! 一切正常!

If you DO NOT use the hashtag and request https://wei.insa-cvl.org/policy directly, your server tries to locate a file or folder named policy but it doesn't exist and the request fails (error 500). 如果不使用主题标签并直接请求https://wei.insa-cvl.org/policy ,则服务器将尝试找到名为policy的文件或文件夹,但该文件或文件夹不存在,并且请求失败(错误500)。

The solution is to configure your server to redirect ALL REQUESTS to the index.html file , so that your Angular app can be executed. 解决方案是将服务器配置为将所有请求重定向到index.html文件 ,以便可以执行Angular应用。

It looks like you are using Apache. 看起来您正在使用Apache。 I found the following code to redirect all requests to index.html (source: https://serverfault.com/a/188411 ): 我发现以下代码将所有请求重定向到index.html (源: https : //serverfault.com/a/188411 ):

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule . /index.html [R=302,L]

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

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