开发者

How to 301 redirect

开发者 https://www.devze.com 2022-12-12 23:51 出处:网络
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.

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/

we want to redirect any requests that contain old.example.com to 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/

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

I know you can do this in IIS 7 or programmatically. 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:

Pattern开发者_如何学编程:

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.


Here's a post describing how to match one domain and redirect to another with everything else in tact using the IIS7 URL Rewrite add-in: http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx


You don't need any complicated rewriting stuff to do such a trivial redirect, it's a basic web server feature. 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.

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. For this site hit the ‘HTTP Redirect’ option and ‘Redirect requests to this destination:’ http://new.example.com/ with ‘Status code:’ 301.

In config XML terms:

<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. You can write the routine to replace the domain names as needed with regex or string.replace() or whatever you prefer.

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. It allows you to enter regular expressions and specify the new URL with back references, e.g. something like this (untested):

<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). If you want to redirect your complete domain, you are probably better off doing this on IIS level rather than on application level. For this, however, you might get better help on http://serverfault.com, which is where the sysadmins reside...

0

精彩评论

暂无评论...
验证码 换一张
取 消