简体   繁体   English

.Net Url用特定的路径模式重写到另一个域

[英].Net Url Rewrite to another domain with a specific path pattern

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) 我正在开发一个新站点来替换我的旧站点,我的目的是将一些旧站点链接重定向到备份服务器(我将旧站点保留一段时间,直到找出所有网址路由)

for example, here are some of the old links https://www.example.com/category/something/here 例如,这是一些旧链接https://www.example.com/category/something/here

I want my new site redirect the above urls to https://backup.example.com/category/something/here 我希望新网站将上述网址重定向到https://backup.example.com/category/something/here

I'm not good at Regex, I tried something like this but not working, any ideas? 我不擅长Regex,我尝试过类似的方法,但是没有用,有什么想法吗? tks ks

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

The url can only match the path. url只能匹配路径。 To match the actual host name, you need to use {HTTP_HOST} . 要匹配实际的主机名,您需要使用{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>

I also put in an example of how to turn off caching for 301 redirects. 我还举了一个示例,说明如何关闭301重定向的缓存。 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. 这可能会导致调试过程中出现问题,因为浏览器将缓存301重定向本身,然后忽略对该配置所做的任何更改,除非手动清除了缓存。

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM