2012-05-01 15 views
40

मैं कुछ वर्णनात्मक यूआरएल को अधिक वर्णनात्मक लोगों के साथ रीडायरेक्ट करने का प्रयास कर रहा हूं। ये यूआरएल .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> 

लेकिन जब से यह सिर्फ विस्तार के साथ खत्म नहीं हुई है, यह काम नहीं किया। क्या यह काम करने के लिए एक आसान तरीका है? मैं आईआईएस 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

मुझे आईआईएस यूआरएल रीवाइट मॉड्यूल 2.0 पसंद है (http://www.iis.net/download/urlrewrite) इस तरह के पुनर्लेखन के लिए बहुत कुछ। – Styxxy

+0

@ mug4n क्या आपको काम करने के लिए पुराने पृष्ठों (services.htm) को रखने की आवश्यकता है या फिर आप अपनी परियोजना से पूरी तरह से हटा सकते हैं? – Dhaust

+0

हाँ आप पुरानी परियोजना फाइलों को हटा सकते हैं – MUG4N

21

शायद आप URL Rewrite जैसे कुछ को httpRedirect का उपयोग करने के बजाय URL को फिर से लिखने के लिए कुछ लिखना चाहते हैं। तो आप इस प्रकार का नियम बना सकता है:

<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 श्रेणी/श्रेणी नाम/1234 पर रीडायरेक्ट)। क्या यह वही बात होगी? और {आर: 2} क्या करता है? –

+0

@PearBerry मुझे पता है कि यह देर हो चुकी है, लेकिन हाँ आप इसे इसी तरह से कर सकते हैं। '{R: 2} 'दूसरे कैप्चर समूह (' ([_0-9a-z -] +)' को संदर्भित करता है ') और जो कुछ भी वहां पर कब्जा कर लिया गया था उसे ले जाता है और इसे पुनः लिखने वाले यूआरएल में बराबर चिह्न के बाद रखता है। – Dannnno

+0

मेरे पास समान स्थिति थी, लेकिन कुछ विफल होने के अनुरोध को रोकें। यह उत्तर मेरे लिए काम करता है: '<नियम सक्षम =" सत्य "नाम =" कॉन्फ़िगरेशन निकालें "> <मिलान अनदेखा करें =" सत्य "url =" configs.json "/> <क्रिया स्थिति कोड =" 404 "प्रकार =" AbortRequest "/> ' – mihkov

0

मामले में जिसे आप जोड़ना http कई साइटों में रीडायरेक्ट करने, आप एसी # सांत्वना कार्यक्रम के रूप में इसका इस्तेमाल कर सकते की जरूरत है:

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; 
     } 
    } 
} 
संबंधित मुद्दे