In the process of developing ASP. NET MVC Web application, after developing the new system, we need to put the old system's URL redirect the new system
There is a file 301redirectspages.config under the project system directory, which reads as follows:
<Rewritemaps> <Rewritemapname= "redirects"> <AddKey= "/contact-us.aspx"value= "/contact-us" /> <AddKey= "/services-solutions"value= "/services-and-solutions" /> <AddKey= "/sales-support"value= "/sales-and-support" /> <AddKey= "/events-news"value= "/events-and-news" /> <AddKey= "/services-solutions/services.aspx"value= "/services-and-solutions/services" /> <AddKey= "/services-solutions/solutions.aspx"value= "/services-and-solutions/solutions" /> </Rewritemap></rewritemaps>
In the Web. config file, under System.webserver, there is a rewrite node that appears as follows:
<system.webserver> <rewriteXdt:transform= "Replace"> <RewritemapsConfigSource= "301redirectspages.config" /> <rules> <Rulename= "Redirect to HTTPS"stopprocessing= "true"> <MatchURL="(.*)" /> <conditions> <Addinput= "{HTTPS}"pattern= "^off$" /> </conditions> <Actiontype= "Redirect"URL= "Https://{http_host}/{r:1}" /> </Rule> <Rulename= "Remove trailing slash"stopprocessing= "true"> <MatchURL="(.*)/$" /> <conditions> <Addinput= "{request_filename}"MatchType= "Isfile"negate= "true" /> <Addinput= "{request_filename}"MatchType= "Isdirectory"negate= "true" /> </conditions> <Actiontype= "Redirect"Redirecttype= "Permanent"URL= "{r:1}" /> </Rule> <Rulename= "redirects"> <MatchURL=".*" /> <conditions> <Addinput= "{Redirects:{request_uri}}"pattern="(.+)" /> </conditions> <Actiontype= "Redirect"URL= "{c:1}"appendquerystring= "true" /> </Rule> </rules> </rewrite></system.webserver>
In this case, enter the URL in the browser window:
Localhost/contact-us.aspx will automatically redirect to Localhost/contact-us
Localhost/services-solutions/services.aspx Automatic Redirect to Localhost/services-and-solutions/services
Localhost/services-solutions/solutions.aspx Automatic Redirect to Localhost/services-and-solutions/solutions
However, when you enter
Localhost/services-solutions cannot automatically redirect to Localhost/services-and-solutions
Localhost/sales-support cannot automatically redirect to Localhost/sales-and-support
Localhost/events-news cannot automatically redirect to Localhost/events-and-news
Find a half-day reason to discover that the settings are in Web. config
Appendquerystring= "True" should be changed to appendquerystring= "false"
This is the line <action type= "Redirect" url= "{c:1}" appendquerystring= "true"/>
Should be changed to <action type= "Redirect" url= "{c:1}" appendquerystring= "false"/>
The following Web. config file is changed as follows:
<rewriteXdt:transform= "Replace"> <RewritemapsConfigSource= "301redirectspages.config" /> <rules> <Rulename= "Redirect to HTTPS"stopprocessing= "true"> <MatchURL="(.*)" /> <conditions> <Addinput= "{HTTPS}"pattern= "^off$" /> </conditions> <Actiontype= "Redirect"URL= "Https://{http_host}/{r:1}" /> </Rule> <Rulename= "Remove trailing slash"stopprocessing= "true"> <MatchURL="(.*)/$" /> <conditions> <Addinput= "{request_filename}"MatchType= "Isfile"negate= "true" /> <Addinput= "{request_filename}"MatchType= "Isdirectory"negate= "true" /> </conditions> <Actiontype= "Redirect"Redirecttype= "Permanent"URL= "{r:1}" /> </Rule> <Rulename= "redirects"> <MatchURL=".*" /> <conditions> <Addinput= "{Redirects:{request_uri}}"pattern="(.+)" /> </conditions> <Actiontype= "Redirect"URL= "{c:1}"appendquerystring= "false" /> </Rule> </rules> </rewrite>
redirect in ASP. NET Web Application