简体   繁体   中英

How to redirect www.example.com to example.com?

My MVC4 app is hosted on Azure (shared website) and works fine at exaple.com.
I'd like to redirect www.example.com to example.com, but it reports 404 error if accessed at www.example.com.

What should I add and is it the proper way to solve this problem?
I don't think it's DNS problem as I have asked it on serverfault .

I have tried to redirect via Web.config:

<system.webServer>
<rewrite>
  <rules>
    <clear />
    <rule name="WWW Rewrite" stopProcessing="true" enabled="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www[.](.+)" />
      </conditions>
      <action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
</system.webServer>

But it doesn't work.

You must have a DNS record for both: www.mysite.com and mysite.com, either A or CNAME. Then, on the web server you should setup a "301" redirect that would redirect all requests from www.mysite.com to mysite.com.

If you don't mind people using both, then just a CNAME record in your DNS zone should do the trick:

www IN CNAME mysite.com

I think there is a way to register with Google and tell it to always use one or the other to index your site.

In addition to the rewrite section in Web.config both domain names (example.com and www.example.com) have to be configured in Azure as custom domains.
Then everything works as desired.

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