0

I have an application hosted within another website using IIS 8.0 like so (In case it's relevant, it's an Angular 2 project using CLI and Webpack). I'm hosting my app at https://example.org/MyApp. Unfortunately, when I try to run the site, I receive a 404 error for each file I have referenced. The browser is trying to look for my files at https://example.org/file.jpg when they are actually hosted at https://example.org/MyApp/file.jpg. Is there any way to tell IIS to search for these files in the subdirectory instead of the root folder?

As a note, Webpack uses relative file paths for all bundling, so changing these file paths to be absolute is not feasible.

Kilo
  • 37
  • 1
  • 6

2 Answers2

0

Edit your index.html to set the <base href>

For example <base href="/MyApp/">

  • Can you explain why this solution fixes the problem? What exactly does it do? – Ross Feb 14 '17 at 21:09
  • 1
    Unfortunately, this doesn't solve the issue. Everything located in my assets folder (https://example.org/MyApp/assets/file.jpg) still returns 404 Not Found because the site is looking for them in https://example.org/assets/file.jpg instead. – Kilo Feb 14 '17 at 22:14
0

Maybe i'm beating a dead horse here, however this seems to work pretty solid:

Basically a mixup of setting base-href and IIS-url-rewrite.

  1. build ng-app with --base-href /myAppDir/
  2. Ensure URL-Rewrite module is available for your IIS-Website
  3. add a web.config-file to the website's root folder.
  4. edit file from 3. to be like, for example:
<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="/MyApp/" />
            <!--<action type="Rewrite" url="/" />-->
          </rule>
        </rules>
      </rewrite>
    </system.webServer>
    </configuration>
  1. You most likely want to use another regular expression as match-url to match your website's needs.

Taken from: https://devblogs.microsoft.com/premier-developer/tips-for-running-an-angular-app-in-iis/

Malte
  • 114
  • 9
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Johan Apr 30 '19 at 06:46
  • Thanks, Johan; you're totally right. Edited the post. – Malte Apr 30 '19 at 07:24