简体   繁体   English

如何301重定向

[英]How to 301 redirect

Ok, I need to know how to do a redirect (where to put the code or specify the setting). 好的,我需要知道如何进行重定向(在哪里放置代码或指定设置)。 We're redirecting from one app to another after we've moved the app. 移动应用后,我们将从一个应用重定向到另一应用。

So for example, if a user goes to existing.example.com/archive/ 因此,例如,如果用户转到了existing.example.com/archive/ 。example.com/ existing.example.com/archive/

we want to redirect any requests that contain old.example.com to new.example.com . 我们希望将包含old.example.com所有请求重定向到new.example.com The rest in the url stays the same. 网址中的其余部分保持不变。 So for example /archive/ is one example so we'd want to redirect them to the new location of this app which is new.example.com/archive/ 因此,例如/archive/是一个示例,因此我们希望将它们重定向到此应用的新位置,即new.example.com/archive/

I need to figure out how to check to see if the incoming URL to our existing site has existing.example.com , and if so replace that part only with the new new.example.com and keep the rest of what's in the url 我需要弄清楚如何检查,看看是否传入的URL,以我们现有的网站有existing.example.com ,如果是更换部分仅与新new.example.com并保持一个什么样的url中休息

I know you can do this in IIS 7 or programmatically. 我知道您可以在IIS 7中或以编程方式进行此操作。 I guess I'm not understanding how to do this in either situation. 我猜我不明白在任何一种情况下如何做到这一点。 I installed the IIS7 Rewrite plugin and ok fine, but here's what I don't get: 我安装了IIS7 Rewrite插件,一切正常,但是我没有得到:

Pattern: 图案:

RedirectURL: 的redirectUrl:

I don't see how in that interface I am able to match the existing.example.com and then what to put in the RedirectURL because I want to put the entire URL with only that existing.example.com changed to new.example.com in for the REdirectURL... and I don't see how I'd do this in IIS 7. 我看不到如何在该界面中匹配existing.example.com ,然后再匹配RedirectURL中的内容,因为我只想将整个URL更改为仅将existing.example.com更改为new.example.com输入REdirectURL ...,我看不到在IIS 7中如何做到这一点。

这是一篇介绍如何使用IIS7 URL重写外接程序匹配一个域并重定向到其他域的文章: http : //weblogs.asp.net/owscott/archive/2009/11/27/iis-url -rewrite重写,非www到www.aspx

You don't need any complicated rewriting stuff to do such a trivial redirect, it's a basic web server feature. 您不需要任何复杂的重写工作即可进行如此简单的重定向,这是Web服务器的基本功能。 In IIS7 you can now choose not to install it (from World Wide Web Services->Common HTTP Features->HTTP Redirection), but it would be unusual to do so. 在IIS7中,您现在可以选择不安装它(从“万维网服务”->“通用HTTP功能”->“ HTTP重定向”),但是这样做并不常见。

Edit the 'Bindings' of the main web site in IIS Manager so that it only responds to the 'Host name:' new.example.com , then create a new web site bound to host name old.example.com . 在IIS管理器中编辑主网站的“绑定”,使其仅响应“主机名:” new.example.com ,然后创建一个绑定到主机名old.example.com的新网站。 For this site hit the 'HTTP Redirect' option and 'Redirect requests to this destination:' http://new.example.com/ with 'Status code:' 301. 对于此站点,请点击“ HTTP重定向”选项和“将请求重定向到此目标:” http://new.example.com/并使用“状态代码:” 301。

In config XML terms: 用配置XML术语:

<system.webServer>
    <httpRedirect enabled="true" destination="http://new.example.com/" httpResponseStatus="Permanent" />
</system.webServer>

In your site's global.asax.cs file you can redirect from BeginRequest as follows. 在站点的global.asax.cs文件中,您可以按照以下方法从BeginRequest进行重定向。 You can write the routine to replace the domain names as needed with regex or string.replace() or whatever you prefer. 您可以编写例程以根据需要用regex或string.replace()或任何您喜欢的名称替换域名。

protected void Application_BeginRequest(Object sender, EventArgs e){ 
    ...parse urls... 
    Response.Redirect(myNewPath)
}

For asp.net applications, I've had good experience with urlrewriting.net , a free component where you can configure redirects in your web.config . 对于asp.net应用程序,我在urlrewriting.net (这是一个免费组件,可以在web.config配置重定向)方面拥有丰富的经验。 It allows you to enter regular expressions and specify the new URL with back references, eg something like this (untested): 它允许您输入正则表达式并指定带有反向引用的新URL,例如,如下所示(未经测试):

<add name="newdomain" virtualUrl="^http\://old.example.com/(.*)$"
  rewriteUrlParameter="ExcludeFromClientQueryString"
  destinationUrl="http://new.example.com/$1"
  redirect="Domain"
  redirectMode="Permanent"
  ignoreCase="true" /> 

Drawback: This needs to be configured for every ASP.NET application (and you might need to reconfigure IIS to route everything through asp.net, or it might only work for .aspx files). 缺点:需要为每个ASP.NET应用程序进行配置(并且您可能需要重新配置IIS以通过asp.net路由所有内容,或者它可能仅适用于.aspx文件)。 If you want to redirect your complete domain, you are probably better off doing this on IIS level rather than on application level. 如果要重定向整个域,最好在IIS级别而不是在应用程序级别执行此操作。 For this, however, you might get better help on http://serverfault.com , which is where the sysadmins reside... 但是,为此,您可能会在http://serverfault.com上获得更好的帮助,这是系统管理员所在的位置...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM