如何让 IIS 支持 vue 多页面路由

如何让 IIS 支持 vue 多页面路由


现在流行的前端框架比如 vue, react都是采用单页面开发,例如一个真实的文件地址和若干个虚拟的路由地址组成,如果使用 hash访问除了丑一点外省了配置的功夫,但就是太丑了所以很多人会用 history模式,不过造成的问题就是打包后部署刷新就会出现 404

从HTML5开始,History interface提供了两个新的方法:pushState(), replaceState()使得我们可以对浏览器历史记录栈进行修改当调用他们修改浏览器历史记录栈后,虽然当前URL改变了,但浏览器不会立即发送请求该URL( the browser won’t attempt to load this URL after a call to pushState() )

具体原理可以移步下面这篇文章从vue-router看前端路由的两种实现

配置

关于下载的步骤流程之前有篇文章已经写过了,这里就不再重复
点击添加规则
在这里插入图片描述
在这里插入图片描述
最后可以看到如下界面,
Requested URL选择 matches the pattern
Using这里选择正则表达式
pattern写正则,从/后开始匹配,当时这里卡了我好几个小时,硬是匹配不上,后面发现 a.com/open/login 其实是从 open/login 开始匹配的,比如我这段正则就是匹配 open开头的所有地址
logical grouping这里选择match All因为我们匹配的不是个文件和文件夹
最后在Action里选择Rewrite ,在Rewrite url中填入要跳转的login.html,这样子所有访问open开头的都会跳转到login.html
在这里插入图片描述

最后web.config 生成的规则如下,有3个主页面,一个是登陆open登陆页,account登陆后查看的页面,404无需登陆的页面。也可以新建.htaccess直接导入,配置文件也会自动更新

				<rule name="open" stopProcessing="true">
                    <match url="(open\/.*)"/>
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="login.html" />
                  </rule>
                  <rule name="account" stopProcessing="true">
                    <match url="(account\/.*)"/>
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="account.html" />
                  </rule>
                  <rule name="404" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/" />
                  </rule>

拓展
官方文档
详解IIS中URL重写工具的规则条件(Rule conditions)
详解IIS中URL重写工具的匹配URL-规则模式(rule patterns)
详解IIS中的重写工具下关于操作重定向URL中的{R:N}与{C:N}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值