如何配置 IIS 以在 HTML5 模式下重写 AngularJS 应用程序的 URL? [英] How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode?

查看:25
本文介绍了如何配置 IIS 以在 HTML5 模式下重写 AngularJS 应用程序的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 AngularJS 种子项目,并且我添加了

I have the AngularJS seed project and I've added

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

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

to the app.js file. I want to configure IIS 7 to route all requests to

http://localhost/app/index.html

所以这对我有用.我该怎么做?

so that this works for me. How do I do this?

更新:

我刚刚发现、下载并安装了 IIS URL 重写模块,希望这能让我的目标更容易实现.

I've just discovered, downloaded and installed the IIS URL Rewrite module, hoping this will make it easy and obvious to achieve my goal.

更新 2:

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

I guess this sums up what I'm trying to achieve (taken from the AngularJS Developer documentation):

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

Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html)

更新 3:

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

I'm still working on this and I realise I need to NOT redirect (have rules that rewrite) certain URLs such as

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

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

so anything that is in the css, js, lib or partials directories isn't redirected. Everything else will need to be redirected to app/index.html

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

Anyone know how to achieve this easily without having to add a rule for every single file?

更新 4:

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

I have 2 inbound rules defined in the IIS URL Rewrite module. The first rule is:

第二条规则是:

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

Now when I navigate to localhost/app/view1 it loads the page, however the supporting files (the ones in the css, js, lib and partials directories) are also being rewritten to the app/index.html page - so everything is coming back as the index.html page no matter what URL is used. I guess this means my first rule, that is supposed to prevent these URLs from being processed by the second rule, isn't working.. any ideas? ...anyone? ...I feel so alone... :-(

推荐答案

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

The IIS inbound rules as shown in the question DO work. I had to clear the browser cache and add the following line in the top of my <head> section of the index.html page:

<base href="/myApplication/app/" />

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

This is because I have more than one application in localhost and so requests to other partials were being taken to localhost/app/view1 instead of localhost/myApplication/app/view1

希望这对某人有所帮助!

Hopefully this helps someone!

这篇关于如何配置 IIS 以在 HTML5 模式下重写 AngularJS 应用程序的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆