301使用web.config将一个域重定向到另一个域 [英] 301 Redirect one domain to another using web.config

查看:229
本文介绍了301使用web.config将一个域重定向到另一个域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个域指向一个托管位置。
我希望将其中一个域建立为我的主域名,因此每当用户从辅助域名访问我的网站时,我希望执行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 or www.test.com/anypage etc.

然后我希望将用户重定向到示例该页面的版本。

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:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

匹配所有网址,除非主机名部分正好是www.example.com - 并将其重定向到www .example.com /无论如何。

Match all urls unless the host name part is exactly www.example.com - and redirect those to www.example.com/whatever.

这篇关于301使用web.config将一个域重定向到另一个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆