簡體   English   中英

.Net Url用特定的路徑模式重寫到另一個域

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

我正在開發一個新站點來替換我的舊站點,我的目的是將一些舊站點鏈接重定向到備份服務器(我將舊站點保留一段時間,直到找出所有網址路由)

例如,這是一些舊鏈接https://www.example.com/category/something/here

我希望新網站將上述網址重定向到https://backup.example.com/category/something/here

我不擅長Regex,我嘗試過類似的方法,但是沒有用,有什么想法嗎? 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>

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重定向本身,然后忽略對該配置所做的任何更改,除非手動清除了緩存。

參考文獻:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM