Question

All I want to do is 301 redirect from old URLs to new URLs:

from /xx/yy/somefile.html to /xx/yy/somefile.aspx

some examples below:

add key="/products/DSD-72B-SP-summary.html" value="/products/DSD-72B-SP-summary.aspx" 
add key="/products/DSD-72B-SP-detail" value="/products/DSD-72B-SP-detail.aspx" 
add key="index.html" value="default.aspx" 
add key="/product-selector.html" value="/products.aspx" 

That is all but it doesn't seem to want to work in IIS 7.5 with URL rewrite 2.0.

I have tried at least 10-20 different rules, and rewrite map formats without any luck. In fact I have done this so many times I have had to wipe the rules and maps from IIS and totally recopy a web.config file from a backup to unscrew what I screwed with to try and make it work.

All I need is a simple rule that tells IIS that if it gets a request for a *.html file to display the *.aspx file that replaced the html file.

    <?xml version="1.0" encoding="UTF-8"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
      <CipherData>

      </CipherData>
    </EncryptedData>
  </appSettings>
  <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
      <CipherData>

      </CipherData>
    </EncryptedData>
  </connectionStrings>

  <system.web>

    <customErrors mode="On" defaultRedirect="404-NotFound.aspx">
      <error statusCode="404" redirect="404-NotFound.aspx" />
      <!--<error statusCode="403" redirect=""/>-->
    </customErrors>

    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
            <!--<codeSubDirectories>
                <add directoryName="CSharp"/>
                <add directoryName="VB"/>
            </codeSubDirectories>-->
        </compilation>
    <authentication mode="Forms">

    </authentication>

    <membership>
      <providers>
        <clear />

      </providers>
    </membership>

    <profile>
      <providers>
        <clear />

      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear />

      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true">

        </modules>

        <defaultDocument>
            <files>

            </files>
        </defaultDocument>


    <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/404-NotFound.aspx" responseMode="ExecuteURL" />
    </httpErrors>
        <tracing>
            <traceFailedRequests>
                <add path="*">
                    <traceAreas>
                        <add provider="WWW Server" areas="Rewrite" verbosity="Verbose" />
                    </traceAreas>
                    <failureDefinitions statusCodes="200-500" />
                </add>
            </traceFailedRequests>
        </tracing>

  </system.webServer>
</configuration>
Was it helpful?

Solution

Make sure you clear your cache too. Sometimes you can update a server rule but your browser will continue to show the old page.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
        <rules> 
            <rule name="Vanity URL" enabled="true">
                 <match url=".*" />
                 <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                     <add input="{MAPNAME:{PATH_INFO}}" pattern="(.+)" />
                 </conditions>
                 <action type="Redirect" url="{C:1}" appendQueryString="false" />
            </rule>
        </rules>
        <rewriteMaps>
            <rewriteMap name="MAPNAME">
                <add key="/products/DSD-72B-SP-summary.html" value="/products/DSD-72B-SP-summary.aspx" />
        </rewriteMap>
    </rewrite>
</system.webServer>
</configuration>

The above code it taking directly from my website with minor name changes for generality and added in your page. Try to remove other rules and isolate the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top