Home > Software engineering >  How to write a general rule to redirect all user urls to one valid Url by using asp.net mvc 5?
How to write a general rule to redirect all user urls to one valid Url by using asp.net mvc 5?

Time:12-07

I want to redirect if user type some Urls in the browser. My application is developed in Asp.net MVC 5.

please see the possible user inputs in the browsers User can enter

1).  test-systems.co.uk
2).  www.test-systems.co.uk
3).  https://test-systems.co.uk
4).  https://www.test-systems.co.uk
5).  http://www.test-systems.co.uk

All above url should be redirected to

https://www.test-systems.co.uk

I have tried the above rule but all URLs are not working as expected

This is my web.config rule detail

<rewrite>
    <rules>
      <clear /> 
        <rule name="Rewrite subdomain" enabled="true" stopProcessing="true">
         <match url="(.*)" /> 
         <conditions>
          <add input="{HTTP_HOST}" type="Pattern" pattern="^(www.)?\.test-systems.co.uk:4005$" /> 
           <add input="{HTTPS}" pattern="^OFF$" />
          <add input="{URL}" pattern="(.*)" />
         </conditions>
         <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent"  /> 
        </rule>
         <rewriteMaps>
      <rewriteMap name="MapProtocol">
        <add key="on" value="https" />
        <add key="off" value="http" />
      </rewriteMap>
    </rewriteMaps>
</rewrite>

CodePudding user response:

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="http to https" 
enabled="true" 
patternSyntax="Wildcard" 
stopProcessing="true">
<match url="*" />
<conditions 
logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" 
/>
</conditions>
<action type="Redirect" 
url="https://{HTTP_HOST{REQUEST_URI}" redirectType="Found" 
/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
  • Related