Aman
Aman

Reputation: 142

Rewrite Rules in separate config file

I am using URL rewriting in my application, I have two config file as given below, first one have configuration and second one have for Rules. but I am getting 404 Error.

Web Config

  <system.webServer>
    <rewrite>
      <rewriteMaps configSource="rewritemaps.config"></rewriteMaps>
    </rewrite>
  </system.webServer>

rewritemaps.config File

<?xml version="1.0"?>
<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>

  <system.webServer>
    <rewrite>
      <rules> 

        <rule name="RewriteURLHometPaging" stopProcessing="true">
          <match url="^Home" />
          <action appendQueryString="false" type="Rewrite" url="Default.aspx" />
        </rule>
        <rule name="RedirectURLHomePaging" stopProcessing="true">
          <match url="^Default\.aspx$" />
          <action appendQueryString="false" type="Redirect" url="Home" />
        </rule>

        <rule name="RedirectURLContactPaging" stopProcessing="true">
          <match url="^Contact-Us\.aspx$" />
          <action appendQueryString="false" type="Redirect" url="Contactus" />
        </rule>

        <rule name="RewriteURLContactPaging" stopProcessing="true">
          <match url="^Contactus" />
          <action appendQueryString="false" type="Rewrite" url="Contact-Us.aspx" />
        </rule>

        <rule name="RedirectURLAboutPaging" stopProcessing="true">
          <match url="^About-Us\.aspx$" />
          <action appendQueryString="false" type="Redirect" url="About" />
        </rule>

        <rule name="RewriteURLAboutPaging" stopProcessing="true">
          <match url="^About" />
          <action appendQueryString="false" type="Rewrite" url="About-Us.aspx" />
        </rule>

      </rules>
    </rewrite>
  </system.webServer>

</configuration>

Error Screenshot is here: Error Screenshot

Upvotes: 5

Views: 3671

Answers (2)

Jonas &#196;ppelgran
Jonas &#196;ppelgran

Reputation: 2747

Here is how to create an external file with one or more rewrite maps (source ruslany.net).

You could also use this technique for creating an external file for rewrite rules. See the end the full post on ruslany.net linked above.

Web.config:

<configuration>
<system.webServer>
  <rewrite>
    <rewriteMaps configSource="MyRewriteMaps.config"></rewriteMaps>
    <rules>
      <rule name="Redirect rule for MyRedirects">
        <match url=".*" />
        <conditions>
          <add input="{MyRedirects:{REQUEST_URI}}" pattern="(.+)" />
        </conditions>
        <action type="Redirect" url="{C:1}" appendQueryString="false" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>

MyRewriteMaps.config:

<rewriteMaps>
  <rewriteMap name="MyRedirects">
    <add key="/oldurl" value="/newurl" />
    <add key="/otheroldurl" value="/othernewurl" />
  </rewriteMap>
</rewriteMaps>

Upvotes: 1

Kishor Mishra
Kishor Mishra

Reputation: 85

Multiple Config Files

The appSettings element can contain a file attribute that points to an external file. It will change web.config file to look like the following:

<appSettings/>

<connectionStrings/>

<system.web>

    <compilation debug="false" strict="false" explicit="true" />

</system.web>

<appSettings file="externalSettings.config"/>

</configuration>

Upvotes: 2

Related Questions