sanjay kumar
sanjay kumar

Reputation: 858

Getting 404 error on page refresh in Angular 4

We have deployed angular 4 application using 'ng build --prod' cli command and created 'dist' folder hosted on IIS 8.5, when we run the application then it's working fine but if pressed F5 key then getting 404 error.

Note: please don't suggest to use (useHash:true) if suggested then how we can remove '#' from url.

Appreciated help if some one can suggest us.

Upvotes: 4

Views: 3792

Answers (1)

bipin patel
bipin patel

Reputation: 2111

You can write change your web.config file like below

<configuration>
    <system.webServer>
         <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" />
                </conditions>
                <action type="Rewrite" url="/" />
              </rule>
            </rules>
          </rewrite>
          <caching enabled="true" enableKernelCache="true">
              <profiles>
                  <add extension=".js" policy="DisableCache" kernelCachePolicy="DisableCache" />
              </profiles>
          </caching>
    </system.webServer>
</configuration>

let's i explain more this configuration

<match url=".*" />

this match all url which is coming and than it's redirect in your base path using action

<action type="Rewrite" url="/" />

If you have set your project in subfolder than change your url="/subfolder/".

Upvotes: 1

Related Questions