2012-05-01 134 views
40

我試圖用一些更具描述性的URL來重定向一些不友好的URL。這些網址以.aspx?cid=3916結尾,每個類別名稱頁面的最後幾位數字不同。我希望它重定向到Category/CategoryName/3916。我在web.config文件試過這樣:在web.config文件中設置重定向

<location path="Category.aspx?cid=3916"> 
<system.webServer> 
    <httpRedirect enabled="true" destination="http://www.site.com/Category/CategoryName/3916" httpResponseStatus="Permanent" /> 
</system.webServer> 

,但因爲它沒有隻用擴展名結尾,它沒有工作。有沒有簡單的方法來使這個工作?我正在使用IIS 7.5。

+0

此選項需要IIS7 https://blogs.msdn.microsoft.com/kaushal/2013/05/22/http-to-https-redirects-on-iis-7-x-and-higher/ –

回答

44
  1. 目錄當舊頁駐留
  2. 那麼對於舊位置的路徑和新的目的地添加代碼如下打開web.config中:

    <configuration> 
        <location path="services.htm"> 
        <system.webServer> 
         <httpRedirect enabled="true" destination="http://domain.com/services" httpResponseStatus="Permanent" /> 
        </system.webServer> 
        </location> 
        <location path="products.htm"> 
        <system.webServer> 
         <httpRedirect enabled="true" destination="http://domain.com/products" httpResponseStatus="Permanent" /> 
        </system.webServer> 
        </location> 
    </configuration> 
    

您可能根據需要添加儘可能多的位置路徑。

+0

我喜歡IIS URL重寫模塊2.0( http://www.iis.net/download/urlrewrite)這些重寫很多。 – Styxxy

+0

@ mug4n您是否需要保留原來的頁面(services.htm)才能使其正常工作,或者您是否可以完全從項目中刪除? – Dhaust

+0

是的,您可以刪除舊的項目文件 – MUG4N

21

你可能想看看URL Rewrite之類的東西,以便將網址重寫爲更友好的網址,而不是使用簡單的httpRedirect。然後,你可以做這樣的規則:

<system.webServer> 
    <rewrite> 
    <rules> 
     <rule name="Rewrite to Category"> 
     <match url="^Category/([_0-9a-z-]+)/([_0-9a-z-]+)" /> 
     <action type="Rewrite" url="category.aspx?cid={R:2}" /> 
     </rule> 
    </rules> 
    </rewrite> 
</system.webServer> 
+0

其實,我試圖做相反的(使category.aspx? cid = 1234重定向到category/categoryname/1234)。它會是同一件事嗎?那麼{R:2}做什麼? –

+0

@PearBerry我知道這很晚了,但是你可以用類似的方式做到這一點。 '{R:2}'指的是第二個捕獲組('([_0-9a-z-] +)'),並將所捕獲的所有內容放在重寫的url中的等號後面。 – Dannnno

+0

我有類似的情況,但只是停止某些失敗的請求。這個答案適用於我: ' ' – mihkov

0

如果你需要添加HTTP在很多網站重定向,你可以使用它作爲一個C#控制檯程序:

class Program 
{ 
    static int Main(string[] args) 
    { 
     if (args.Length < 3) 
     { 
      Console.WriteLine("Please enter an argument: for example insert-redirect ./web.config http://stackoverflow.com"); 
      return 1; 
     } 

     if (args.Length == 3) 
     { 
      if (args[0].ToLower() == "-insert-redirect") 
      { 
       var path = args[1]; 
       var value = args[2]; 

       if (InsertRedirect(path, value)) 
        Console.WriteLine("Redirect added."); 
       return 0; 
      } 
     } 

     Console.WriteLine("Wrong parameters."); 
     return 1; 

    } 

    static bool InsertRedirect(string path, string value) 
    { 
     try 
     { 
      XmlDocument doc = new XmlDocument(); 

      doc.Load(path); 

      // This should find the appSettings node (should be only one): 
      XmlNode nodeAppSettings = doc.SelectSingleNode("//system.webServer"); 

      var existNode = nodeAppSettings.SelectSingleNode("httpRedirect"); 
      if (existNode != null) 
       return false; 

      // Create new <add> node 
      XmlNode nodeNewKey = doc.CreateElement("httpRedirect"); 

      XmlAttribute attributeEnable = doc.CreateAttribute("enabled"); 
      XmlAttribute attributeDestination = doc.CreateAttribute("destination"); 
      //XmlAttribute attributeResponseStatus = doc.CreateAttribute("httpResponseStatus"); 

      // Assign values to both - the key and the value attributes: 

      attributeEnable.Value = "true"; 
      attributeDestination.Value = value; 
      //attributeResponseStatus.Value = "Permanent"; 

      // Add both attributes to the newly created node: 
      nodeNewKey.Attributes.Append(attributeEnable); 
      nodeNewKey.Attributes.Append(attributeDestination); 
      //nodeNewKey.Attributes.Append(attributeResponseStatus); 

      // Add the node under the 
      nodeAppSettings.AppendChild(nodeNewKey); 
      doc.Save(path); 

      return true; 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine($"Exception adding redirect: {e.Message}"); 
      return false; 
     } 
    } 
}