Angular deployment - 404 on page Refresh

后端 未结 4 1180
小蘑菇
小蘑菇 2021-01-07 11:50

i would like to deploy my app in Angular to production server but I have problems. App works corectly when I use only angular routing (change component, not redirecting) but

相关标签:
4条回答
  • 2021-01-07 12:19

    My initial workaround was to just redirect on 404 back to root, and so far, I don't see a down-side to this. To my surprise, both the request path and parameters persist when refreshing. This is in an IIS 10 environment with Angular 9, and a Chrome-only (internal app) so there may be other factors at play that I'm unaware of that are only making this seem sustainable. Will try to remember to come back to my answer should we discover something.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <httpErrors>
                <remove statusCode="404" subStatusCode="-1" />
                <error statusCode="404" prefixLanguageFilePath="" path="/" responseMode="ExecuteURL" />
            </httpErrors>
        </system.webServer>
    </configuration>
    
    0 讨论(0)
  • 2021-01-07 12:22

    If you are using angular 6,7 this method works (If you are okay with /#/ in your URL.

    In app.module.ts

    import {LocationStrategy, HashLocationStrategy} from '@angular/common';
    

    After import add following line to providers.

    {provide: LocationStrategy, useClass: HashLocationStrategy}
    

    ex:

    providers: [AuthService, 
                AuthGuard, 
                FlxUiDataTable,
                {provide: LocationStrategy, useClass: HashLocationStrategy}]
    

    This will solve your issue. Read Documentation here.

    0 讨论(0)
  • 2021-01-07 12:24

    I modified the web.config in my app:

    <configuration>
      <system.webServer>
    <rewrite>
        <rules>
            <rule name="redirect all" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
                </conditions>
                <action type="Rewrite" url="./" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>
      </system.webServer>
    </configuration>
    

    In index.html is <base href="./">. Refreshing the page is now ok.

    0 讨论(0)
  • 2021-01-07 12:36

    This work for angular 8, add .htaccess to your project file

    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    
    RewriteEngine On
    
    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
    
    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [L]
    

    0 讨论(0)
提交回复
热议问题