问题描述
我有多个域指向一个托管位置.我希望将其中一个域建立为我的主域,因此我希望在用户从辅助域访问我的网站时执行 301 重定向到该主域.
I have multiple domains pointing to one hosting location.I wish to establish one of the domains as my main domain and therefore I wish to perform a 301 redirect to this main domain whenever a user accesses my site from a secondary domain.
例如:
www.example.com
www.example.com
这是我的主要域名.我希望与我的网站关联的所有其他域都重定向到此处.
This is my main domain. I want all other domains associated with my site to redirect to here.
如果用户进来:
www.test.com 或www.test.com/anypage等
www.test.com orwww.test.com/anypageetc.
然后我希望用户被重定向到该页面的示例版本.
Then I want the user to be redirected to the example version of that page.
如何使用应用程序的 web.Config 文件执行此操作?我问的原因是,通常我的网络托管服务提供商在他们的后台有一个工具,允许我设置此重定向,但是,我们的客户选择了不提供此类工具的其他托管服务提供商.
How do I do this using the web.Config file of my application? The reason I ask is that usually my web hosting provider has a tool in their back office that allows me to setup this redirect however, our client has opted for a different hosting provider that do not provide such a tool.
我尝试使用以下代码执行此重定向,但似乎不起作用:
I have attempted to do this redirect using the following code but it doesn't seem to work:
<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 重定向.
My application is an Umbraco powered site and so has several system.webServer entries in the web.config file. It may just be the case that I have entered this code in the wrong place but any help here would be greatly appreciated as I am only used to doing 301 redirects in .htaccess files.
推荐答案
这与 umbraco 无关,但我认为您想要做的是:
This is not really that umbraco related but I think what you want to do is this:
<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>
匹配所有网址,除非主机名部分恰好是 www.example.com - 并将这些网址重定向到 www.example.com/whatever.
Match all urls unless the host name part is exactly www.example.com - and redirect those to www.example.com/whatever.
这篇关于301 使用 web.config 将一个域重定向到另一个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!