3

I have an Multiculture MVC2 website. Actually my home page can be accessed with the following paths:

http://mydomain.com
http://mydomain.com/
http://mydomain.com/en
http://mydomain.com/en/
http://mydomain.com/en/home
http://mydomain.com/en/home/

What I want is that all the above paths make a 301 redirect to the following:

http://mydomain.com/en

so that I don't have to share pagerank between different urls.

Note that the en string is dynamic and sets the culture for the website.

I'm new in Asp.Net MVC, someone could post some code to do that? Thanks

opaera
  • 31
  • 1
  • 3

2 Answers2

2

You can create a custom action result. See this thread: http://forums.asp.net/p/1337938/2700733.aspx

Jimmy Bosse
  • 1,344
  • 1
  • 12
  • 24
2

something like this

public class PermanentRedirectResult : ViewResult
{
    public string Url { get; set; }

    public PermanentRedirectResult(string url)
    {
        if (string.IsNullOrEmpty(url))
            throw new ArgumentException("url is null or empty", url);
        this.Url = url;
    }

    public override void ExecuteResult(ControllerContext context)
    {
      if (context == null)
        throw new ArgumentNullException("context");
      context.HttpContext.Response.StatusCode = 301;
      context.HttpContext.Response.RedirectLocation = Url;
      context.HttpContext.Response.End();
    }
}

and call it with this

return new PermanentRedirectResult("/myurl");

Rippo
  • 22,117
  • 14
  • 78
  • 117
  • To downvoter? Why downvote this answer 2yrs after it was posted, at the very least you could do is provide a comment? Also remember this is MVC version 2! – Rippo Jan 09 '13 at 09:17