2012-05-01 9 views
40

私はいくつかの不公平なURLを、より説明的なURLにリダイレクトしようとしています。これらのURLは.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> 

それは拡張子だけで終わらなかったので、それは動作しませんでした。これを動作させる簡単な方法はありますか?私はIIS 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

私はIIS URL書き換えモジュール2.0が好きですhttp://www.iis.net/download/urlrewrite)これらの書き換えの多くに。 – Styxxy

+0

@ mug4nこれを行うには古いページ(services.htm)を保持する必要がありますか、プロジェクトから完全に削除することはできますか? – Dhaust

+0

はい、古いプロジェクトファイル – MUG4N

21

単純なhttpRedirectではなく、よりユーザーフレンドリーなものにURLを書き換えるには、おそらくURL Rewriteのようなものを見たいと思うでしょう。次に、このようにルールを作ることができる:あなたはHTTPを追加する必要がある場合には

<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にリダイレクト)。それは同じことでしょうか? {R:2}は何をするのですか? –

+0

@PearBerry私はこれが遅いと知っていますが、はい、同様の方法でそれを行うことができます。 '{[R:2}} 'は2番目の捕捉グループ('([_0-9a-z - ] +) ')を参照し、そこに捕捉されたものをとり、書き換えられたURLの等号の後に置きます。 – Dannnno

+0

私は同様の状況を抱えていましたが、特定の失敗の要求を停止します。この回答は私のために働いています: ' mihkov

0

はあなたが交流#コンソールプログラムとしてそれを使用することができ、多くのサイトにリダイレクト:

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; 
     } 
    } 
} 
関連する問題