source

web.config 파일에서 리디렉션 설정

bestscript 2023. 8. 28. 21:47

web.config 파일에서 리디렉션 설정

나는 몇몇 비우호적인 URL을 좀 더 설명적인 URL로 리디렉션하려고 합니다.은 "URL"로 ..aspx?cid=3916각 범주 이름 페이지의 마지막 숫자가 서로 다릅니다.대신 다음으로 리디렉션하기를 원합니다.Category/CategoryName/3916는 했습니다.web.config파일 이름:

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

연장만으로 끝나지 않았기 때문에 효과가 없었습니다.이것을 쉽게 작동시킬 수 있는 방법이 있습니까?IIS 7.5를 사용하고 있습니다.

  1. 을 엽니다.web.config이전 페이지가 있는 디렉토리에서

  2. 그런 다음 다음과 같이 이전 위치 경로 및 새 대상에 대한 코드를 추가합니다.

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

필요한 만큼 위치 경로를 추가할 수 있습니다.

당신은 URL Rewrite와 같은 것을 보고 단순한 것을 사용하는 것보다 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>

많은 사이트에서 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;
        }
    }
}

언급URL : https://stackoverflow.com/questions/10399932/setting-up-redirect-in-web-config-file