首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iis 10中添加Url重定向规则

如何在iis 10中添加Url重定向规则
EN

Stack Overflow用户
提问于 2020-07-30 23:27:02
回答 1查看 81关注 0票数 0

我想在IIS 10中写重定向规则。我在谷歌上搜索了它,花了半天时间想出答案,但没有运气,所以我把它发了出来,以得到一些解决方案。

  1. https://testing.app.com/apptest应该重定向到https://testing.app.com/apptest/account/login
  2. https://testing.app.com/apptest/应该重定向到https://testing.app.com/apptest/account/login
  3. https://test-apptest.testing.app.com/应该重定向到https://test-apptest.testing.app.com/account/login
  4. https://test-apptest.testing.app.com应该重定向到https://test-apptest.testing.app.com/account/login

但是当用户输入url、https://testing.app.com/apptest/account/loginhttps://test-apptest.testing.app.com/account/login时,它不应该重定向到任何地方,应该保持原样。

代码语言:javascript
复制
      <rewrite>
            <rules>
                <rule name="Test1" stopProcessing="true">
                    <match url="account/login" />                   
                    <action type="None" />
                </rule>  
                <rule name="Test2">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="testing.app.com" />
                        <add input="{REQUEST_URI}" pattern="^/apptest" />
                    </conditions>
                    <action type="Redirect" url="https://testing.app.com/apptest/account/login" appendQueryString="false" />
                </rule>
                
            </rules>
        </rewrite>  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-31 03:20:32

我们只需在正则表达式中添加一个锚点,以便与段‘/apptest’精确匹配。

更新的

代码语言:javascript
复制
  <system.webServer>
        <rewrite>
            <rules>
                <rule name="MyRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_URI}" pattern="^((/apptest)?)/?$" />
                    </conditions>
                    <action type="Redirect" url="https://{http_host}{C:1}/account/login" />
                </rule>
            </rules>
        </rewrite>
</system.webServer>

解释

由于主机名已更改,随后将被追加到重定向URL中,因此我将其替换为{http_host}服务器变量以跟随它。此外,{Request_URI}将返回URL路径,{C:1}将返回"/apptest"""。因此,我将其附加到重定向URL中。

每个服务器变量的含义。

https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule

最后,请不要忘记在应用规则之前安装URL重写扩展名。

https://www.iis.net/downloads/microsoft/url-rewrite

下面是Microsoft文档中正则表达式的快速引用。

https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

如果问题仍然存在,请随时通知我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63182798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档