2016-12-16 82 views
1

我已成功創建並運行angualr2-cli應用程序。我也在apache服務器上部署應用程序,爲此,我使用angular2-cli命令的幫助ng build在Angualr 2 Cli應用程序中構建後刷新顯示404錯誤

但是,我的應用程序在刷新子頁面時出現故障(404 Page Not Found Error)。

請執行以下步驟,您將意識到我提到的問題。

  1. Click this link to load the web page

  2. 單擊作業邊欄菜單或任何或側邊欄元素

  3. 然後刷新頁面

您將能看到我的問題。該頁面消失並顯示404錯誤。爲什麼在部署到Apache服務器後發生這種情況。刷新完全適用於開發環境(http://localhost:4200),而我使用的是ng serve命令運行服務器

回答

2

我已經在this guide後修復此問題。這在我的配置中沒有錯。問題在於添加.httaccess並允許在服務器設置上重寫規則。

我總結了我已經採取的措施來解決這個問題。

  1. 我在index.html所在位置添加了.httaccess文件。

    <IfModule mod_rewrite.c> 
        RewriteEngine On 
        RewriteBase/
        RewriteRule ^index\.html$ - [L] 
        RewriteCond %{REQUEST_FILENAME} !-f 
        RewriteCond %{REQUEST_FILENAME} !-d 
        RewriteRule . /index.html [L] 
    </IfModule> 
    

2.然後讓Apache服務器上rewite國防部使用sudo a2enmod rewrite

3.重新啓動Apache服務器service apache2 restart

4.After打開的/etc/apache2/sites-enabled/000-default.conf文件,並添加以下規則<VirtualHost>標籤

<Directory "/var/www/html"> 
    AllowOverride All 
</Directory> 

就是這樣!任何鏈接都會重定向到我的index.html並起作用。因爲所有的航線都在angular2路由

+0

我試過這個我自己,這個工程。 –

+0

https://github.com/angular/angular-cli/issues/1630 摘錄是的,基本思想是服務器根據文件加載路由,但是你的整個角度應用運行在index.html上,它告訴瀏覽器顯示哪條路徑,所以使用htaccess,你幾乎可以將所有服務器傳入的流量路由到index.html,並且它可以順利運行:) –

0

這裏從index.html的路線是IIS服務器的解決方案

  1. 創建的web.config文件,該文件包含以下內容:

    <!-- configure the site to work with HTML5 push state --> 
    <rewrite> 
        <rules> 
        <rule name="AngularJS Routes" stopProcessing="true"> 
         <match url=".*" /> 
         <conditions logicalGrouping="MatchAll"> 
         <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
         <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" /> 
         </conditions> 
         <action type="Rewrite" url="/" /> 
        </rule> 
        </rules> 
    </rewrite> 
    

  2. 將此文件放入yor root部署的文件夾中。