user3111321
user3111321

Reputation: 55

have error 404 in redirect angular app via address bar in browser

have error 404 in redirect angular app via the address bar in browser but when does this work in the browser after running angular app via ng serve command everything is ok. my config in Nginx is:

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                root “D:\Tools\nginx-1.16.0\nginx-1.16.0\html”;
                try_files $uri $uri/ /index.html?$args;
       }

Note: Currently Define base in Index html file

 <base href="/">

Upvotes: 0

Views: 348

Answers (1)

Irshad Ali
Irshad Ali

Reputation: 1213

You have two options, you can use any that suits you well:

  1. Make changes in Nginx server config rule to redirect.
  2. Use HashLocationStrategy of Angular

#1

I don't know the config setting for Nginx but for IIS. You could find some equivalent of below:

<system.webServer>
 <rewrite>
      <rules>
        <rule name="redirect all requests" 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="samcds/index.html" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

2.

In your app module, add below providers:

 providers: [Location, {provide: LocationStrategy, useClass: HashLocationStrategy}], 

for referenece https://angular.io/api/common/HashLocationStrategy

Upvotes: 2

Related Questions