我有多个域指向一个托管位置。
我希望将其中一个域建立为我的主域,因此我希望在用户从辅助域访问我的站点时执行 301 重定向到该主域。
例如:
www.example.com
这是我的主域。我希望与我的网站关联的所有其他域都重定向到这里。
如果用户进来:
www.test.com 或
www.test.com/anypage
等等。
然后我希望用户被重定向到该页面的示例版本。
如何使用应用程序的 web.Config 文件执行此操作?我问的原因是,通常我的网络托管服务提供商在他们的后台有一个工具,允许我设置此重定向,但是,我们的客户选择了不提供此类工具的其他托管服务提供商。
我尝试使用以下代码进行此重定向,但似乎不起作用:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^test\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}}" redirectType="Permanent" />
</rule>
我的应用程序是一个 Umbraco 驱动的站点,因此在 web.config 文件中有几个 system.webServer 条目。可能只是我在错误的地方输入了这段代码,但这里的任何帮助将不胜感激,因为我只习惯于在 .htaccess 文件中进行 301 重定向。
最佳答案
这并不是真的与 umbraco 相关,但我认为您想要做的是:
<rewrite>
<rules>
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
匹配所有 url,除非主机名部分恰好是 www.example.com - 并将它们重定向到 www.example.com/whatever。
关于redirect - 301 使用 web.config 将一个域重定向到另一个域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32648449/