2012-05-01 17 views
40

Próbuję przekierować niektóre nieprzyjazne adresy z bardziej opisowymi. Adresy te kończą się .aspx?cid=3916, a ostatnie cyfry są różne dla każdej strony z nazwą kategorii. Chcę, aby zamiast tego przekierować do Category/CategoryName/3916. Próbowałem to w pliku web.config:Konfigurowanie przekierowania w pliku 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> 

ale ponieważ nie kończy się tylko rozszerzeniem, to nie działa. Czy istnieje prosty sposób, aby to zadziałało? Używam usług IIS 7.5.

+0

Ta opcja wymaga IIS7 https://blogs.msdn.microsoft.com/kaushal/2013/05/22/http-to-https-redirects-on-iis-7-x-and-higher/ –

Odpowiedz

44
  1. Otwarte web.config w katalogu gdzie stare strony zamieszkują
  2. Następnie dodać kod dla starej i nowej lokalizacji ścieżki przeznaczenia, jak następuje:

    <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> 
    

Użytkownik może dodaj tyle ścieżek lokalizacji, ile potrzeba.

+0

Podoba mi się IIS URL Rewrite Module 2.0 (http://www.iis.net/download/urlrewrite) wiele do tego rodzaju przepisywania. – Styxxy

+0

@ mug4n Czy chcesz zachować stare strony (serwisy.htm), aby to działało, czy możesz je całkowicie usunąć z projektu? – Dhaust

+0

Tak, możesz usunąć stare pliki projektu – MUG4N

21

Prawdopodobnie chcesz spojrzeć na coś takiego, jak URL Rewrite, aby przepisać adresy URL na bardziej przyjazne dla użytkownika, zamiast używać prostego httpRedirect. Następnie można zrobić regułę tak:

<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

Właściwie, próbuję zrobić coś przeciwnego (zrobić category.aspx? cid = 1234 przekierowanie do kategorii/nazwy kategorii/1234). Czy to było to samo? A co robi {R: 2}? –

+0

@PearBerry Wiem, że jest późno, ale tak, możesz to zrobić w podobny sposób. '{R: 2}' odnosi się do drugiej grupy przechwytującej ('([_0-9a-z -] +)') i bierze wszystko, co zostało tam przechwycone i umieszcza ją po znaku równości w przepisanym adresie URL. – Dannnno

+0

Miałem podobną sytuację, ale po prostu zatrzymuję prośbę o pewne niepowodzenie. Ta odpowiedź działa dla mnie: ' ' – mihkov

0

W przypadku, gdy trzeba dodać http przekierować na wiele stron, można użyć go jako C# programu konsoli:

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; 
     } 
    } 
} 
Powiązane problemy