简体   繁体   English

如何将www.example.com重定向到example.com?

[英]How to redirect www.example.com to example.com?

My MVC4 app is hosted on Azure (shared website) and works fine at exaple.com. 我的MVC4应用托管在Azure(共享网站)上,并在exaple.com上运行良好。
I'd like to redirect www.example.com to example.com, but it reports 404 error if accessed at www.example.com. 我想将www.example.com重定向到example.com,但是如果访问www.example.com,它将报告404错误。

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 . 我认为这不是DNS问题,正如我在serverfault上提出的那样

I have tried to redirect via Web.config: 我试图通过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. 您必须同时具有以下两者的DNS记录:www.mysite.com和mysite.com(A或CNAME)。 Then, on the web server you should setup a "301" redirect that would redirect all requests from www.mysite.com to mysite.com. 然后,在Web服务器上,您应该设置“ 301”重定向,该重定向会将所有请求从www.mysite.com重定向到mysite.com。

If you don't mind people using both, then just a CNAME record in your DNS zone should do the trick: 如果您不介意同时使用这两种方法的人,那么只需在DNS区域中的CNAME记录即可:

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. 我认为有一种方法可以向Google注册,并告诉它始终使用其中一个来索引您的网站。

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. 除了Web.config中的重写部分外,还必须在Azure中将两个域名(example.com和www.example.com)配置为自定义域。
Then everything works as desired. 然后一切都会按需工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将非www或非http重定向到https://www.example.com - How to redirect non www or non http to https://www.example.com 将所有来自www.example.com的流量重定向到IIS7中的特定页面 - redirect all traffic coming from www.example.com to specific page in IIS7 路线网址,例如http://www.example.com/{userId} - Route URL such as http://www.example.com/{userId} 如何从http://www.example.com/images/business/113.jpg获取数字 - How to get numbers from http://www.example.com/images/business/113.jpg 如何为GET请求的C#规则重写example.com/123重定向到example.com/default.aspx?pid=123和123是动态更改 - How to rewritten rule for c# for GET request example.com/123 redirect to example.com/default.aspx?pid=123 and 123 is dynamic change window.setTimeout(&#39;window.location =“ http://www.example.com”;&#39;,2000); 在后面的代码中 - window.setTimeout('window.location=“http://www.example.com”; ', 2000); in code behind 如何添加类似于example.com/id的MapRoute - How to add a MapRoute like example.com/id 捕获和识别HttpRequestException“无法解析远程名称:&#39;www.example.com&#39;”的正确方法是什么? - What is the proper way to catch and identify the HttpRequestException “The remote name could not be resolved: 'www.example.com'”? 像http://example.com这样的输入网址在操作输入中更改为http:/example.com - Input URL like http://example.com changes to http:/example.com in action input 如何使用 articleName=sample-article-name [MVC Core2.2] 将“www.example.com/sample-article-name”路由到 RazorPage Index.cshtmll - How to route to "www.example.com/sample-article-name" to RazorPage Index.cshtmll with articleName=sample-article-name [MVC Core2.2]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM