我有一个简单的应用程序运行在IIS 8.0(arvixe托管)上,它使用AngularJS作为SPA框架。我打开了html5mode并将标签链接到index.html(根文件),我还将此文件设置为web.config中的。我知道你必须做我有的服务器端重写。
我想要实现的是以下几点:
用户输入"domain.com",网站加载 domain.com/home。当然,在URL下实际上是 domain.com/index.html/home 但是html5mode通过重写来处理它。
这是app.config,这很好。
.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider ) {
$routeProvider
.when('/home', {
templateUrl: 'templates/home.html',
controller: 'HomeController'
})
.when('/login', {
templateUrl: 'templates/login.html',
controller: 'LoginController'
})
.when('/forum', {
templateUrl: 'templates/forum.html',
controller: 'ForumController'
})
.when('/rebellinks', {
templateUrl: 'templates/rebellinks.html',
controller: 'RebelLinksController'
})
.when('/events', {
templateUrl: 'templates/events.html',
controller: 'EventsController'
})
.when('/oldnews', {
templateUrl: 'templates/oldnews.html',
controller: 'OldNewsController'
})
.otherwise({
redirectTo: '/home'
});
//use HTML5 History API
$locationProvider.html5Mode(true);
}]);
IIS 的 Web.配置文件:
<system.webServer>
<rewrite>
<rules>
<clear />
<!--<rule name="jsRule" stopProcessing="true">
<match url=".js" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="None" />
</rule>-->
<rule name="SRBShome" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<action type="Rewrite" url="/index.html" />
</rule>
<rule name="AngularJS" enabled="false" 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>
<defaultDocument enabled="true">
<files>
<clear />
<add value="index.html" />
<add value="home.html" />
</files>
</defaultDocument>
这样做是在用户访问站点时加载 domain.com/home 但.js文件被传输,因为我在重写中添加了一个特殊规则以防止它们重写,但我认为我在 SRBShome 规则上的模式需要调整才能正常工作。
任何帮助表示赞赏。
通过更改下面的重写规则解决了这个问题。这允许在 IIS 上加载索引.html。由于某种原因,web.config 不适用于此设置。所以现在当用户请求重写传递'/index.html domain.com 时,AngularJS的html5Mode就会启动并清理URL并删除hashbang。
<rewrite>
<rules>
<clear />
<rule name="SRBShome" 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"/>
<!--<match url="^.com" />
<action type="Rewrite" url="/index.html" />-->
</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>