23

I deployed an application developed using angular 6 on IIS of windows server. Now I am facing one issue that on navigating the app if I am refreshing the page then it says 404 - File or directory not found. I looked at the posts related to this issue and tried to apply fix. but no luck. I do not want to use # with my urls. I just want to use urls simply like http://example.xyz.com/detail/12

If I am using # then its fine but I don't want to use #.

I tried to add a web.config as well, but no luck. Can any one help here how to fix this issue so that on refresh same page can be opened that I want to reload.

 <?xml version="1.0" encoding="utf-8"?>
<configuration>

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Angular 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="./index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

</configuration>
4
  • 1
    You should configure iis to route to index.html on unknown urls, since it thinks your angular route is a folder on server, and returns 404 since in reality there is no such folder Commented Dec 4, 2018 at 12:07
  • how it can be done? plz explain. as index.html already there in default documents in IIS Commented Dec 4, 2018 at 13:18
  • I am not iis master but like this i guess stackoverflow.com/a/43787391/6528560 Commented Dec 4, 2018 at 13:24
  • yes its works stackoverflow.com/questions/43785928/… Commented Dec 7, 2018 at 10:59

1 Answer 1

53

Follow this link to fix this issue Angular 2 Hosted on IIS: HTTP Error 404 or just

Step 1: Install IIS URL Rewrite Module

Step 2: Add a rewrite rule to web.config

<?xml version="1.0" encoding="utf-8"?>
    <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>
      </system.webServer>
    </configuration>
Sign up to request clarification or add additional context in comments.

2 Comments

The above works even on Azure hosting, just need to add web.config file in "wwwroot" directory, Default web.config file will not available in on angular build --prod, So manually we need to add the web.config file
Hey I tried your weboconfig but always returns Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.