2017-10-10 98 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和備份託管從兩個單獨的服務器 –

相關問題