2012-05-01 74 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; 
     } 
    } 
} 
相关问题