Search code examples
asp.netweb-configiis-10

Web.Config Url rewrite works but Redirect not working


I have the following web.config Where I have added the URL Rewrite and redirect rules. Which is partially working.

Code:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <caching enabled="false" />
        
        <rewrite>
        
          <rules>
          
              
            <rule name="Rewrite to pages.asp">
              <match url="^pages/([_0-9a-z-]+)" />
              <action type="Rewrite" url="pages.asp?p={R:1}" />
            </rule>
            
            <rule name="Redirect from pages.asp">
                <match url="^pages/([_0-9a-z-]+)" />
                <action type="Redirect" url="pages/{R:1}" redirectType="Permanent" />
            </rule>
            
          </rules>
        </rewrite>
            
    </system.webServer>
</configuration>

The URL structure is like this:

https://www.example.com/pages.asp?p=my-article

The rewrite should be like this (and it works):

https://www.example.com/pages/my-article/

if I manually visit this URL it works but the redirect part is not working as it should automatically. Something is wrong with this code.


Solution

  • The rules were all written wrong, by using the IIS rewrite rule manager, I have a working code now. This might help someone else with the same issue.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <caching enabled="false" />
            
            <rewrite>
                <rules>
                    
                    <rule name="RedirectPages" stopProcessing="true">
                        <match url="^pages\.asp$" />
                        <conditions>
                            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                            <add input="{QUERY_STRING}" pattern="^p=([^=&amp;]+)$" />
                        </conditions>
                        <action type="Redirect" url="pages/{C:1}" appendQueryString="false" />
                    </rule>
                    <rule name="RewritePages" stopProcessing="true">
                        <match url="^pages/([^/]+)/?$" />
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="pages.asp?p={R:1}" />
                    </rule>
                </rules>
                <outboundRules>
                    <rule name="OutboundPages" preCondition="ResponseIsHtml1">
                        <match filterByTags="A, Form, Img" pattern="^(.*/)pages\.asp\?p=([^=&amp;]+)$" />
                        <action type="Rewrite" value="{R:1}pages/{R:2}/" />
                    </rule>
                    
                </outboundRules>
              
              
            </rewrite>
                
        </system.webServer>
    </configuration>