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

mehmood tekfirst 771 Reputation points
2022-12-05T14:21:59.817+00:00

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>  
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2022-12-05T16:12:01.343+00:00

    Contact the folks that manage your DNS and ask them to add records for test-systems.co.uk and www.test-systems.co.uk . If DNS is already done, then simply configure the bindings in IIS.

    Configuring Http to https redirect on IIS is a Google search away. I've used the following for years and if I recall I just used the GUI interface in IIS.

    <?xml version="1.0" encoding="UTF-8"?>  
    <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>  
    

    Of course, you firewall must allow port 80 traffic.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.