IIS Rewrite for Specified Url And Port not working

Viewed 22

Im trying to handle IIS Rewrite for Specific website and port.

This is my Setting in web.config (Im Using Asp.net MVC)

    <rewrite>
        <rules>
            <rule name="test" stopProcessing="true">
                <match url="^(https?:\/\/)?127\.0\.0\.1:80\/?$" />
                <conditions>
                </conditions>
                <action type="Redirect" url="http://google.com" />
            </rule>
        </rules>
    </rewrite>

and when i call address http://127.0.0.1 it should redicret me to google but it wont.

if i change it like this it works :

    <rewrite>
        <rules>
            <rule name="test" stopProcessing="true">
                <match url=(.*) />
                <conditions>
                </conditions>
                <action type="Redirect" url="http://google.com" />
            </rule>
        </rules>
    </rewrite>

the pattren works perfectly on my inbound rule :

enter image description here

adding condition also didnt help :

    <rewrite>
        <rules>
            <rule name="test" stopProcessing="true">
                <match url="^(https?:\/\/)?127\.0\.0\.1:80\/?$" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^(https?:\/\/)?127\.0\.0\.1:80\/?$" />
                </conditions>
                <action type="Redirect" url="http://google.com" />
            </rule>
        </rules>
    </rewrite>

ive enabled the FRT to check log and it shows this :

<failedRequest url="http://127.0.0.1:80/" siteId="1" appPoolId="DefaultAppPool" 
processId="8432" verb="GET" remoteUserName="" userName="" tokenUserName="NT AUTHORITY\IUSR" 
authenticationType="anonymous" activityId="{800008BA-0001-E700-B63F-84710C7967BB}" 
failureReason="STATUS_CODE" statusCode="302" triggerStatusCode="302" timeTaken="0" 
xmlns:freb="http://schemas.microsoft.com/win/2006/06/iis/freb"
2 Answers

you could try below rule:

 <rewrite>
    <rules>
        <rule name="test" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="127.0.0.1" />
            </conditions>
            <action type="Redirect" url="http://test.com" />
        </rule>
    </rules>
</rewrite>

With @LexLi Link i was able to fix it with this :

    <rewrite>
        <rules>
            <rule name="test" stopProcessing="true">
                <match url="^$" />
             
                <action type="Redirect" url="http://www.google.com"  appendQueryString="false"/>
             
            </rule>
        </rules>
    </rewrite> 
Related