【问题标题】:Setting up redirect in web.config file在 web.config 文件中设置重定向
【发布时间】:2012-05-11 02:41:01
【问题描述】:

我正在尝试用更具描述性的网址重定向一些不友好的网址。这些 url 以 .aspx?cid=3916 结尾,每个类别名称页面的最后一位数字不同。我希望它改为重定向到Category/CategoryName/3916。我在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。

【问题讨论】:

标签: asp.net web-config http-redirect


【解决方案1】:
  1. 旧页面所在的目录中打开 web.config
  2. 然后为旧位置路径和新目的地添加代码如下:

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

您可以根据需要添加任意数量的位置路径。

【讨论】:

  • 我非常喜欢 IIS URL 重写模块 2.0 (iis.net/download/urlrewrite) 进行此类重写。
  • @mug4n 您是否需要保留旧页面 (services.htm) 以使其正常工作,还是可以将其从项目中完全删除?
  • 是的,它确实适用于 aspx 文件。有关示例代码,请参见此处:stackoverflow.com/questions/7325831/…
  • httpRedirect 与 URL REWRITE iis.net/download/urlrewrite 的区别?
  • 哪些文件应该保留在 IIS 的“旧”应用程序中,以便重定向继续工作。我的应用有点大,我需要保持原样还是可以删除二进制文件等?
【解决方案2】:

您可能希望查看 URL Rewrite 之类的东西来将 URL 重写为对用户更友好的 URL,而不是使用简单的 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>

【讨论】:

  • 实际上,我正在尝试做相反的事情(使 category.aspx?cid=1234 重定向到 category/categoryname/1234)。会是一样的吗? {R:2} 有什么作用?
  • @PearBerry 我知道这已经晚了,但是是的,您可以以类似的方式做到这一点。 {R:2} 指的是第二个捕获组 (([_0-9a-z-]+)) 并获取在那里捕获的任何内容并将其放在重写 url 中的等号之后。
  • 我有类似的情况,但只是停止请求某些失败。这个答案对我有用:&lt;rule enabled="true" name="Remove Configurations"&gt; &lt;match ignoreCase="true" url="configs.json"/&gt; &lt;action statusCode="404" type="AbortRequest" /&gt; &lt;/rule&gt;
  • 如果我要传递 2 个参数怎么办?我应该如何传递 action type="Redirect" 的 url 我试过这个但是它不允许“&”请帮忙
  • @ShalinJirawla 在 XML 文件中,您需要转义 & 符号。使用&amp;amp;
【解决方案3】:

如果您需要在许多站点中添加 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;
        }
    }
}

【讨论】:

  • 这绝对是一个 Web 配置......您知道 IIS 不必首先托管 .NET 应用程序吗?因此,您的 C# 解决方案完全忽略了这个问题。如果 IIS 用于托管静态内容,则没有 .NET 应用程序在运行。
  • 我有兴趣提供一种编程方式来执行与以前的解决方案相同的操作,仅此而已。此外,可以在以下位置找到类似的方法:docs.microsoft.com/en-us/iis/configuration/system.webserver/…,我不认为我错过了这个问题。
猜你喜欢
  • 2020-04-30
  • 2016-10-22
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 2023-03-16
  • 2015-08-25
相关资源
最近更新 更多