简体   繁体   English

部署到 Apache 的 Angular 6 路由问题(未找到 URL 404)

[英]Angular 6 routing issue with deployment to Apache (URL not found 404)

I have a small Angular 6 app that works as expected when using 'ng serve' and localhost:4200.我有一个小的 Angular 6 应用程序,它在使用“ng serve”和 localhost:4200 时按预期工作。 If I refresh a page or manually type a URL I get the expected results and the page loads.如果我刷新页面或手动输入 URL,我会得到预期的结果并加载页面。

However, when I build a distribution using 'ng build --base-href=/quickorder/' the app runs fine until I do a refresh or type the url in a tab wheen I get URL not found 404 with the URL being shown as the part after the servername.但是,当我使用 'ng build --base-href=/quickorder/' 构建发行版时,应用程序运行良好,直到我刷新或在选项卡中键入 url 当我得到 URL not found 404 且 URL 显示为servername 后面的部分。

The dist is placed in Apache's htdocs in a folder named quickorder and the URL is . dist 放置在 Apache 的 htdocs 中名为 quickorder 的文件夹中,URL 为 . The Apache conf file for the dist is basically standard apart from changing the SRVROOT to reflect Apache's location.除了更改 SRVROOT 以反映 Apache 的位置之外,dist 的 Apache conf 文件基本上是标准的。

app.modules.ts app.modules.ts

 const appRoutes:Routes = [
  {
    path: '',
    component: LoginComponent
  },  
  {
    path: 'order-input/:ref/:name',
    canActivate: [AuthguardGuard],
    component: OrderInputComponent
  },
  {
    path: 'order-input',
    canActivate: [AuthguardGuard],
    component: OrderInputComponent
  },  
  {
    path: 'trad-verify',
    canActivate: [AuthguardGuard],
    component: TradVerifyComponent
  },
  {
    path: '**',
    component: LoginComponent
  } 
 ]

Thanks for any help you can provide me,感谢您为我提供的任何帮助,

Mark.标记。

PS Looking at the Apache docs for 2.4 they recommend not using an .htaccess if you have access to httpd.conf and all .htaccess commands can go into the httpd.conf PS查看2.4的Apache文档,如果您有权访问httpd.conf并且所有.htaccess命令都可以进入httpd.conf,他们建议不要使用.htaccess

<Directory />
   RewriteEngine On
   RewriteBase /quickorder
   # If an existing asset or directory is requested go to it as it is
   RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
   RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
   RewriteRule ^ - [L]

   # If the requested resource doesn't exist, use index.html
   RewriteRule ^ /index.html
   Options FollowSymLinks
   AllowOverride All
   #AllowOverride none
   #Require all denied
</Directory>

However, this gives me an error rresponse from the server;但是,这给了我来自服务器的错误响应; You don't have permission to access /cgi-bin/dugquickorder.cgi/wh on this server.您无权访问此服务器上的 /cgi-bin/dugquickorder.cgi/wh。

Solved.解决了。 In Apache 2.4 an .htaccess file is not required (if you have access to httpd.conf).在 Apache 2.4 中不需要 .htaccess 文件(如果您可以访问 httpd.conf)。 The changes can go inside a 'directory'更改可以进入“目录”

#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other 
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all denied
</Directory>

<Directory C:\Apache\Apache24\htdocs\quickorder\>

RewriteEngine On
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]

    # If the requested resource doesn't exist, use index.html
    RewriteRule ^ \index.html
    AllowOverride All
</Directory>

Refer the following link:请参考以下链接:

https://blog.neoprime.it/ng-in-httpd/ https://blog.neoprime.it/ng-in-httpd/

It helped to resolve the problem.它有助于解决问题。 I have not used any .htaccess file but have overridden httpd.conf .我没有使用任何.htaccess文件,但覆盖了httpd.conf

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

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