简体   繁体   中英

Configure IIS URL Rewrite module for AngularJS HTML5Mode Still Not Working

I have searched at found some info on configuring IIS URL Rewrite module to work with AngularJS HTML5Mode and even tried the info I found on another question:

How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode?

I've made the changes to my IIS as displayed in the question found in the link above and set the base URL and the site works when I visit the root site (ie http: //localhost/appname/). All of my views and the routing works fine.

However; when I try to visit a specific URL (ie http: //localhost/appname/about) in a new tab or browser window, I get a 404. Is there something else I'm missing???

I'm running Windows 7, IIS 7 with the URL Rewrite Module installed.

** I had to space the protocol out to allow the example URL to be accepted.

Install this extension: https://www.iis.net/downloads/microsoft/url-rewrite

And add this to web.config under system.webServer

<rewrite>
  <rules>
    <clear />        
    <rule name="Html5Mode" enabled="true" stopProcessing="true">
      <match url="^(.+)$" negate="true" />
      <conditions>
        <add input="{REQUEST_URL}" pattern="^(.+)$" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.html" logRewrittenUrl="true"/>
    </rule>
    <rule name="AngularJS" enabled="true" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.html" />
    </rule>        
  </rules>
</rewrite>

Here is the full web.config including serving static files.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <clear />        
            <rule name="Html5Mode" enabled="true" stopProcessing="true">
              <match url="^(.+)$" negate="true" />
              <conditions>
                <add input="{REQUEST_URL}" pattern="^(.+)$" negate="true" />
              </conditions>
              <action type="Rewrite" url="/index.html" logRewrittenUrl="true"/>
            </rule>
            <rule name="AngularJS" enabled="true" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
              </conditions>
              <action type="Rewrite" url="/index.html" />
            </rule>        
          </rules>
        </rewrite>
        <handlers>
           <clear />
            <add 
                name="StaticFile" 
                path="*" verb="*" 
                modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" 
                resourceType="Either" 
                requireAccess="Read" />
        </handlers>
        <staticContent>
            <mimeMap fileExtension=".*" mimeType="application/octet-stream" />
        </staticContent>
    </system.webServer>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM