.Net Url用特定的路径模式重写到另一个域 [英] .Net Url Rewrite to another domain with a specific path pattern

查看:48
本文介绍了.Net Url用特定的路径模式重写到另一个域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个新站点来替换我的旧站点,我的目的是将一些旧站点链接重定向到备份服务器(我将使旧站点保持一段时间,直到我确定删除所有网址路由)

I'm developing a new site to replace one of my old site, and my purpose is to redirect some of the old site links to the backup server (I will keep the old site live for a period of time until I figured out all the url routings)

例如,这是一些旧链接 https://www.example.com/category/something/here

for example, here are some of the old links https://www.example.com/category/something/here

我希望我的新站点将上述网址重定向到 https://backup.example.com/category/something/here

I want my new site redirect the above urls to https://backup.example.com/category/something/here

我不擅长Regex,我尝试了类似的方法,但是没有用,有什么想法吗?tks

I'm not good at Regex, I tried something like this but not working, any ideas? tks

    <rule name="Redirect URL" stopProcessing="true">
      <match url="^(.*)/category/(.*)" ignoreCase="true" />
      <action type="Redirect" url="http://backup.example.com/{R:0}" redirectType="Permanent" />
    </rule>

推荐答案

url 只能匹配路径.要匹配实际的主机名,您需要使用 {HTTP_HOST} .

The url can only match the path. To match the actual host name, you need to use {HTTP_HOST}.

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect www.example.com or example.com to backup.example.com" stopProcessing="true">
                    <match url="^category/(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^(www\.)?example.com$" />
                    </conditions>
                    <action type="Redirect" url="http://backup.example.com/category/{R:1}" redirectType="Permanent" />
                </rule>         
            </rules>
        </rewrite>
        <httpProtocol>
            <redirectHeaders>
                <!-- This is to ensure that clients don't cache the 301 itself - this is dangerous because the 301 can't change when put in place once it is cached -->
                <add name="Cache-Control" value="no-cache"/>
            </redirectHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

我还举了一个示例,说明如何为301重定向关闭缓存.这可能会导致调试过程中出现问题,因为浏览器将缓存301重定向本身,然后忽略对该配置所做的任何更改,除非手动清除了缓存.

I also put in an example of how to turn off caching for 301 redirects. This can cause problems during debugging because browsers will cache the 301 redirect itself and then ignore any changes you make to this configuration unless the cache is cleared manually.

参考文献:

这篇关于.Net Url用特定的路径模式重写到另一个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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