简体   繁体   中英

not able to redirect from www.domain.com to domain.com in MVC C#

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:

  1. Logged in into my GoDaddy DNS management page and added a new type "A" record with Host as "www" and To as "IP Address of website", TTL = 1 hour.
  2. After adding new DNS record, I logged into my server using Remote desktop and navigated to my website using IIS manager (Server->Sites (expand it) -> Select your website), Clicked on "Bindings" (inside right hand side panel, below "Edit")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM