2017-10-10 36 views
0

我正在开发一个新站点来替换我的一个旧站点,而我的目的是将一些旧站点链接重定向到备份服务器(我将保留旧的网站活一段时间,直到我想通了所有的URL路由).Net Url用特定路径模式重写到另一个域

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

我想我的新网站上的URL重定向到 https://backup.example.com/category/something/here

我不擅长Regex,我尝试过这样的事情但没有工作,有什么想法? 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> 
+0

我会说你不应该做一个永久重定向,如果是仅是时间问题。 –

+0

@RichardHubley - OP表示“我正在开发一个新网站来替换我的旧网站”。这意味着永久重定向是正确的方法。 – NightOwl888

回答

2

url只能匹配路径。要匹配实际的主机名称,您需要使用{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重定向,然后忽略对此配置所做的任何更改,除非手动清除缓存。

参考文献:

+0

tks的答复,但事件我完全取出R:0,只使用url =“https://backup.example.com”,它仍然不会重新定向,并给我在新网站上的404错误 –

+0

它重定向到了正确的URL吗? – NightOwl888

+0

不,它仍然停留在www.example.com,因为它是新网站,所以我得到了404错误。顺便说一句,www和备份托管从两个单独的服务器 –

相关问题