web-dev-qa-db-ja.com

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.site.com/Category/CategoryName/3916" httpResponseStatus="Permanent" />
</system.webServer>

しかし、拡張子だけでは終わらないため、機能しませんでした。これを機能させる簡単な方法はありますか? IIS 7.5を使用しています。

47
Pear Berry
  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>
    

必要な数のロケーションパスを追加できます。

52
MUG4N

おそらく RL Rewrite のようなものを見て、単純なhttpRedirectを使用するのではなく、より使いやすいURLに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>
24
vcsjones