2012-05-01 32 views
40

Tôi đang cố gắng chuyển hướng một số url không thân thiện với nhiều mô tả hơn. Các url này kết thúc bằng số .aspx?cid=3916 với các chữ số cuối cùng khác nhau cho mỗi trang tên danh mục. Thay vào đó, tôi muốn chuyển hướng đến Category/CategoryName/3916. Tôi cố gắng này trong file web.config:Thiết lập chuyển hướng trong tệp 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> 

nhưng vì nó không kết thúc chỉ với phần mở rộng, nó đã không làm việc. Có cách nào dễ dàng để làm việc này không? Tôi đang sử dụng IIS 7.5.

+0

Tùy chọn này đòi hỏi IIS7 https://blogs.msdn.microsoft.com/kaushal/2013/05/22/http-to-https-redirects-on-iis-7-x-and-higher/ –

Trả lời

44
  1. mở web.config trong thư mục nơi trang cũ cư trú
  2. Sau đó thêm mã cho đường dẫn vị trí cũ và điểm đến mới như sau:

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

Bạn có thể thêm nhiều đường dẫn vị trí nếu cần.

+0

Tôi thích Mô-đun Rewrite URL IIS 2.0 (http://www.iis.net/download/urlrewrite) rất nhiều cho các loại viết lại này. – Styxxy

+0

@ mug4n Bạn có cần phải giữ các trang cũ (services.htm) tại chỗ cho việc này để làm việc hoặc có thể bạn hoàn toàn loại bỏ sau đó từ dự án của bạn? – Dhaust

+0

có bạn có thể xóa các tệp dự án cũ – MUG4N

21

Bạn có thể muốn xem một số thứ như URL Rewrite để viết lại URL cho nhiều người dùng thân thiện hơn là sử dụng đơn giản httpRedirect. Sau đó bạn có thể làm cho một quy tắc như thế này:

<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

Thực ra, tôi đang cố gắng làm ngược lại (tạo category.aspx? cid = 1234 chuyển hướng đến danh mục/tên danh mục/1234). Nó có giống nhau không? Và {R: 2} làm gì? –

+0

@PearBerry Tôi biết điều này là muộn, nhưng có bạn có thể làm điều đó một cách tương tự. '{R: 2}' dùng để chỉ nhóm thứ hai ('([_0-9a-z -] +)') và lấy bất cứ thứ gì được chụp ở đó và đặt nó sau dấu bằng trong url được viết lại. – Dannnno

+0

Tôi có tình huống tương tự, nhưng chỉ dừng yêu cầu cho một số lỗi nhất định. Câu trả lời này phù hợp với tôi: ' ' – mihkov

0

Trong trường hợp đó bạn cần phải thêm http chuyển hướng trong nhiều trang web, bạn có thể sử dụng nó như aC# console chương trình:

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; 
     } 
    } 
} 
Các vấn đề liên quan