Azure网站301重定向 - 我在哪里放?

时间:2013-08-01 20:39:32

标签: asp.net iis redirect azure web-config

我想将其他一些域名引导到我的主域名,该域名是在Windows Azure网站上托管的。

(为了那些发现使用CNAME和DNS的人有点“模糊”(就像我做的那样)我将布置细节。)

我正确地解析了域www.myDomain.com

我现在想指出www.myOtherDomain.com to www.myDomain.com

在我的注册商处,我创建了一个指向的CNAME www.myOtherDomain.com to myInternalAzureSite.azurewebsite.net
然后在Azure网站域管理器工具中成功配置它。

现在,当我在浏览器中输入www.myOtherDomain.com时,我会在www.myDomain.com获得正确的网页,但是,浏览器中的地址仍为www.myOtherDomain.com而非www.myDomain.com为期望的。

我理解实现这一目标的两种最理想的方法是:

  1. 转发myOtherDomain.com(某些注册商的费用为$)
  2. 执行301永久重定向
  3. 如果我完全正确的话,我已经找到了很多 HOW 的建议来进行301重定向,但是,我似乎无法弄明白 WHERE 把重定向?

4 个答案:

答案 0 :(得分:58)

Windows Azure网站运行IIS。您可以使用URL重写来创建规则,将一个URL重写为另一个URL。

说明:

  1. 在Windows Azure中创建网站。

  2. 在“缩放”部分,选择“共享”或“标准”的网站模式并保存更改。

  3. 在“配置”部分的“域名”组中,添加旧域名(或多个名称)和新域名并保存更改。

  4. 在旧域和新域的域名注册商或DNS提供商中,将DNS记录更改为指向新的Windows Azure网站。使用“CNAME(别名)”记录并将其指向Windows Azure上的网站域,如下所示:“mywebsite.azurewebsites.net。”

  5. 将新网站的内容上传到Windows Azure。

  6. 在新网站的根目录中,创建一个名为Web.config的文件,内容如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect old-domain to new-domain" stopProcessing="true">
                        <match url=".*" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^www.old-domain.com$" />
                        </conditions>
                        <action type="Redirect" url="http://www.new-domain.com/{R:0}" redirectType="Permanent" />
                    </rule>              
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    
  7. 确认对“http://www.old-domain.com/path?query”的任何请求都会收到“301 Moved Permanently”响应,其中包含“http://www.new-domain.com/path?query”的位置标题。

  8. 有关文档,请参阅Using the URL Rewrite Module

    有关示例,请参阅Redirect to new domain after rebranding with IIS Url Rewrite ModuleIIS URL Rewrite – Redirect multiple domain names to one

答案 1 :(得分:4)

您也可以将此代码放在web.config节点下的configuration文件中进行重定向:

<configuration>
  <location path="oldpage1.php">
    <system.webServer>
      <httpRedirect enabled="true" destination="http://domain.com/newpage1" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
  <location path="oldpage2.php">
    <system.webServer>
      <httpRedirect enabled="true" destination="http://domain.com/newpage2" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
</configuration>

答案 2 :(得分:4)

无需上传web.config文件,因为您可以通过Azure界面访问该文件。

打开App Service的设置窗格,然后单击左侧菜单底部的“开发工具”部分中的App Service Editor(预览)。

单击“转到”以在新选项卡中打开编辑器。您将在左侧看到web.config文件。单击它在主窗格中进行编辑。

一句警告 - 此编辑器在您键入时自动保存!我确信无论如何我们都会这样做,但我建议你在编辑器中准备你的代码并粘贴它。

我能够添加一个部分而无需手动重启App Service。

答案 3 :(得分:-1)

我采用了一种更简单的方法,并通过“自定义域”菜单将额外的必需域添加到了Azure应用服务-如以下屏幕截图所示。完全不需要触摸主要网站的web.config文件。无需任何显式301重定向。在“自定义域”页面中,只需分配域,并通过上传HTTPS证书来添加SSL绑定-请访问该站点! enter image description here

相关问题