I am trying to redirect from website from https://www.example.com to https://example.com in ASP.NET MVC 5, but I am not able to do it, I am getting this error, when navigating to https://www.example.com
HTTP Error 404. The requested resource is not found.
I have tried to use the solution provided here
https://stackoverflow.com/a/3197446/3559462
https://stackoverflow.com/a/2178381/3559462
Currently my Web.Config has this code for url re-write
<rule name="Redirect everything to https://example.com" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="example.com" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:0}" />
</rule>
What is missing? It use to work previously but not now, why I am getting 404 suddenly and how to re-direct https://www.example.com to https://example.com without getting any error.
Do I need to register any DNS settings in my GoDaddy account? Or C#/web.config code can solve this error.
EDIT : when trying to use url https://example.com it works, here is the request/response headers image
I was able to solve this using these steps:
Then added the new binding with values as Type : https , host-name : www.example.com , Port : 443 , IP Address : IP Address of website,
That's it, after following above steps my problem got resolved and now website is redirected to https://example.com with 301 permanent redirect
Also please note the Web.Config settings (Performs redirect from www to non-www website), which I have already placed in the question, here it is again
<rule name="Redirect everything to https://example.com" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="example.com" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:0}" />
</rule>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.