redirigir redireccionar net iis7 automatically asp asp.net web-config http-redirect

asp.net - redireccionar - redirigir http a https iis7



Configurando la redirección en el archivo web.config (3)

  1. Abra web.config en el directorio donde residen las páginas antiguas
  2. Luego agregue el código para la ruta de ubicación anterior y el nuevo destino de la siguiente manera:

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

Puede agregar tantas rutas de ubicación como sea necesario.

Estoy tratando de redireccionar algunas URL hostiles con más descriptivas. Estas direcciones URL terminan en .aspx?cid=3916 con los últimos dígitos siendo diferentes para cada página de nombre de categoría. Quiero que, en cambio, redirija a Category/CategoryName/3916 . Intenté esto en el archivo 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>

pero como no terminó solo con la extensión, no funcionó. ¿Hay una manera fácil de hacer que esto funcione? Estoy usando IIS 7.5.


En caso de que necesite agregar el redireccionamiento de http en muchos sitios, podría usarlo como un programa de consola ac #:

class Program { static int Main(string[] args) { if (args.Length < 3) { Console.WriteLine("Please enter an argument: for example insert-redirect ./web.config http://.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; } } }


Probablemente quiera ver algo como iis.net/download/urlrewrite para reescribir las URL a otras más fáciles de usar en lugar de usar un simple httpRedirect . Entonces podrías hacer una regla como esta:

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