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

2024-04-28

我有AngularJS 种子项目 https://github.com/angular/angular-seed我已经添加了

$locationProvider.html5Mode(true).hashPrefix('!');

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

http://localhost/app/index.html

这样这对我有用。我该怎么做呢?

Update:

我刚刚发现、下载并安装了IIS URL重写模块 http://www.iis.net/downloads/microsoft/url-rewrite,希望这能让我的目标变得容易和明显。

Update 2:

我想这总结了我想要实现的目标(取自AngularJS 开发者文档 http://docs.angularjs.org/guide/dev_guide.services.%24location):

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

更新3:

我仍在研究这个问题,我意识到我不需要重定向(有重写规则)某些 URL,例如

http://localhost/app/lib/angular/angular.js
http://localhost/app/partials/partial1.html

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

有人知道如何轻松实现这一点,而不必为每个文件添加规则吗?

更新4:

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

第二条规则是:

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


我在 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>

在我的 index.html 中我将其添加到<head>

<base href="/">

不要忘记安装IIS URL 重写 http://www.iis.net/downloads/microsoft/url-rewrite在服务器上。

另外,如果您使用 Web API 和 IIS,那么如果您的 API 位于www.yourdomain.com/api因为第三个输入(条件的第三行)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何配置 IIS 以在 HTML5 模式下 URL 重写 AngularJS 应用程序? 的相关文章

随机推荐