0

I'd like to set up a 301 redirect from a .co.uk site to a .com site, keeping the same pages.

So someone going to mydomain.co.uk/pages/about should get redirected to mydomain.com/pages/about

I'm using ASP.NET and IIS 7.0 and not having much luck. I tried setting a custom 404 file that has a redirect in it like so:

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(".co.uk"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace(
                ".co.uk",
                ".com"));
    }

however my 404 page isn't being called when I go to a URL such as /pages/about. I have the following in my web.config:

<configuration>
  <system.web>
    <customErrors mode="On" defaultRedirect="404.aspx"/>
  </system.web>
</configuration>

Can anyone assist please?

4

3 回答 3

0

customErros section within your web.config will be accessed in case there is an error in your application. Is there any error when try to navigate to mydomain.co.uk/pages/about?

If there is no error - customErrors is not affected. In this case you may try Response.Redirect("mydomain.com/pages/about").

See HttpResponse.Redirect on MSDN (cite: ASP.NET performs the redirection by returning a 302 HTTP status code.)

And have a look at customErrors section and errors element

于 2012-08-25T08:24:52.113 回答
0

I ended up using the Application_BeginRequest function of the global.asax. Much cleaner than trying to redirect to a 404

于 2012-08-26T00:18:55.227 回答
0

please see also http://www.idea-r.it/Blog.aspx/asp-net-301-redirect?utm_source=rss&utm_medium=rss&utm_campaign=rss

it addresses the issue very nicely!

于 2013-02-24T22:31:03.627 回答