ASP.net URL将子目录重写为外部URL [英] ASP.net URL Rewrite subdirectory to external URL

查看:85
本文介绍了ASP.net URL将子目录重写为外部URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要URL将子目录重写到外部域.

示例:访问"

这是一个官方示例.

如果要在不安装ARR扩展的情况下完成任务,则必须使用重定向"操作类型.当我们访问合格的URL时,浏览器地址栏中的URL将会更改.

 < rewrite><规则>< rule name ="Myrules" stopProcessing ="true">< match url =(.*)"/><条件><添加输入="{PATH_INFO}"模式="/test.*"/></conditions>< action type ="Redirect" url ="https://www.bing.com/maps" appendQueryString ="false" logRewriteUrl ="false"/></rule></rules></rewrite> 

请随时告诉我是否有什么可以帮忙的.

I need to URL Rewrite a subdirectory to an external domain.

Example: When visiting "https://example1.com/test", "https://example2.com/hello" should open. The URL should still be "https://example1.com/test".

I tried to solve this by adding a Rewrite rule in web.config, but it don't works. Here is the Rewrite rule I made:

<rule name="TestRule" stopProcessing="true">
  <match url="^test(/.*)?$" />
  <action type="Rewrite" url="https://example2.com/hello" appendQueryString="false" />
</rule>

解决方案

In order to redirect the incoming request to another domain by using Rewrite action type (stay the old URL in the browser address bar), we need to install Application Request Routing module.
https://www.iis.net/downloads/microsoft/application-request-routing
By default, Rewrite action only forwards these requests to the same domain, therefore, we only can specify a URL path in the Rewrite URL field.
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#rewrite-action
Otherwise, Redirecting the incoming request to another domain will cause a 404 error. After we installed the ARR extension, enable Reverse Proxy functionality following the below steps.


Here is an official example.
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing
Finally, please refer to the below configuration.

<rewrite>
    <rules>
        <rule name="Myrules" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{PATH_INFO}" pattern="/test.*" />
            </conditions>
            <action type="Rewrite" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
        </rule>
    </rules>
</rewrite>

It will redirect the requests that has "/test" segment to the https://www.bing.com/maps.

If we want to complete the task without installing ARR extension, we have to use Redirect action type. The URL in the browser address bar will change while we access the qualified URL.

<rewrite>
    <rules>
        <rule name="Myrules" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{PATH_INFO}" pattern="/test.*" />
            </conditions>
            <action type="Redirect" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
        </rule>
    </rules>
</rewrite>

Feel free to let me know if there is anything I can help with.

这篇关于ASP.net URL将子目录重写为外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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