如何在HTML5模式下为URL重写AngularJS应用程序配置IIS?

Dea*_*ean 136 iis url-rewriting angularjs

我有AngularJS种子项目,我已经添加了

$locationProvider.html5Mode(true).hashPrefix('!');
Run Code Online (Sandbox Code Playgroud)

到app.js文件.我想配置IIS 7以将所有请求路由到

http://localhost/app/index.html
Run Code Online (Sandbox Code Playgroud)

这对我有用.我该怎么做呢?

更新:

我刚刚发现,下载并安装了IIS URL Rewrite模块,希望这样可以轻松,明显地实现我的目标.

更新2:

我想这总结了我想要实现的目标(取自AngularJS Developer文档):

使用此模式需要在服务器端重写URL,基本上您必须重写所有链接到应用程序的入口点(例如index.html)

更新3:

我还在努力,我意识到我不需要重定向(有重写规则)某些URL,例如

http://localhost/app/lib/angular/angular.js
http://localhost/app/partials/partial1.html
Run Code Online (Sandbox Code Playgroud)

所以css,js,lib或partials目录中的任何内容都不会被重定向.其他所有内容都需要重定向到app/index.html

任何人都知道如何轻松实现这一点,而无需为每个文件添加规则?

更新4:

我在IIS URL重写模块中定义了2个入站规则.第一条规则是:

IIS URL重写入站规则1

第二条规则是:

IIS URL重写入站规则2

现在,当我导航到localhost/app/view1时,它会加载页面,但是支持文件(css,js,lib和partials目录中的文件)也被重写到app/index.html页面 - 所以一切都将到来无论使用什么URL,都返回index.html页面.我想这意味着我的第一条规则,即应该阻止这些网址被第二条规则处理,不起作用..任何想法?...任何人?...我感觉很孤单... :-(

Yag*_*urk 274

我写了在web.config中后的规则$locationProvider.html5Mode(true)中设置app.js.

希望,帮助别人.

  <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" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
Run Code Online (Sandbox Code Playgroud)

在我的index.html中,我添加了这个 <head>

<base href="/">
Run Code Online (Sandbox Code Playgroud)

不要忘记在服务器上安装IIS URL Rewrite.

此外,如果您使用Web API和IIS,如果您的API www.yourdomain.com/api因为第三个输入(第三行条件)而处于此状态,这将起作用.

  • 这是一个笑话,我们必须跳过Angular路由的这些箍.这会为我杀死角度路由.真是一团糟. (7认同)
  • 完善!这对我来说是一个棘手的问题,花了几个小时研究这个问题.谢谢!建议任何人:我将index.html添加到<action type ="Rewrite"url"/index.html"/>然后在我的index.html上(Angular SPA的根目录文件,<base href ="/ index .html"/>当我处理上面的代码时,它与我的<base href />不匹配,所以它无法正常工作."/(api)"模式的巨大道具也是,我一直在那个部分磕磕绊绊. (5认同)
  • 这非常有用,我发现它是最完整的.谢谢! (2认同)
  • 很高兴它帮助@Mario (2认同)

Dea*_*ean 36

问题DO工作中显示的IIS入站规则.我必须清除浏览器缓存并<head>在index.html页面的部分顶部添加以下行:

<base href="/myApplication/app/" />
Run Code Online (Sandbox Code Playgroud)

这是因为我在localhost中有多个应用程序,因此对其他部分的请求被localhost/app/view1取而代之localhost/myApplication/app/view1

希望这有助于某人!

  • 这也很有用.重装页面时为我工作.没有针对每个文件的任何特殊规则:https://coderwall.com/p/mycbiq (2认同)

Cip*_*anu 10

只有这两个条件的问题:

  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
Run Code Online (Sandbox Code Playgroud)

只要{REQUEST_FILENAME} 物理存在于磁盘上,它们才能工作.这意味着可能存在这样的情况:对错误命名的部分视图的请求将返回根页而不是404将导致角加载两次(并且在某些情况下它可能导致令人讨厌的无限循环).

因此,建议使用一些安全的"后备"规则来避免这些难以解决的问题:

  <add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
  <add input="{REQUEST_FILENAME}" pattern="(.*?)\.js$" negate="true" />
  <add input="{REQUEST_FILENAME}" pattern="(.*?)\.css$" negate="true" />
Run Code Online (Sandbox Code Playgroud)

或者匹配任何文件结尾的条件:

<conditions>
  <!-- ... -->
  <add input="{REQUEST_FILENAME}" pattern=".*\.[\d\w]+$" negate="true" />
</conditions>
Run Code Online (Sandbox Code Playgroud)


Jos*_*h C 9

在我的情况下,在设置了正确的重写规则后,我一直得到403.14.事实证明,我有一个与我的一个URL路由同名的目录.一旦我删除了IsDirectory重写规则,我的路由就能正常工作.是否存在删除目录否定可能导致问题的情况?在我的案例中我无法想到任何事情.我能想到的唯一情况是你是否可以使用你的应用程序浏览目录.

<rule name="fixhtml5mode" stopProcessing="true">
  <match url=".*"/>
  <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="/" />
</rule>
Run Code Online (Sandbox Code Playgroud)