0

recently I've updated my website from AJAX to ASP.MVC5, so the old urls which are still in the search engine need to be removed.

Because my old URLs is AJAX based, Google will not gonna remove them it self since every old urls will re-direct to the homepage , for example :

mysite.com/#!product/phone/samsung/galaxy3
mysite.com/#!product/phone/iphone/ip4s
mysite.com/#!product/phone/nokia/lumia920

Now the solution is return to Google an 401 HTTP error status everytime the server receive a request from the old URL (contains #! or _escaped_fragment_)

How do I return this 401 HTTP error status code back to the requester proactively in MVC5 ? Thanks alot!

NeedAnswers
  • 1,411
  • 3
  • 19
  • 43

3 Answers3

1

You need create Route. In the route write RedirectLocation and return Status

public class NewUrlRoute : RouteBase
      {
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
          const string status = "401 HTTP error status";
          var request = httpContext.Request;
          var response = httpContext.Response;
          var title = "";
          var legacyUrl = request.Url.ToString();
          var newUrl = "";
          var id = request.QueryString.Count != 0 ? request.QueryString[0] : "";

          if (legacyUrl.Contains(" #!"))
          {
            response.Status = status;
            response.RedirectLocation = "newUrl";
            response.End();
          }
            return null;
        }

      }
1

If you want to achieve this in the controller you can just check for the previous url and simply do:

HttpContext.Response.StatusCode = 401;

The routing engine will automatically handle the response header regardless of your return type.

IsakBosman
  • 1,453
  • 12
  • 19
  • How can perform the check and return the status code globally instead of embedding this code in every controllers? – NeedAnswers Nov 11 '14 at 03:09
  • @hoangnnm there are a few ways you can achieve that. You can implement a base controller and override one of the controller request pipeline methods. Look here http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.aspx specifically at BeginExecute, Ecexute etc. or you can look at a custom controller factory where you have access to the routedata and render a response. However I would implement a custom route handler similar to what Dimitry suggested. You can see a goodish example here http://www.codeproject.com/Articles/595520/MvcRouteHandler-and-MvcHandler-in-ASP-NET-MVC-Fram – IsakBosman Nov 11 '14 at 07:18
0

Solved, turned out there is a very easy way : .NET MVC-4 routing with custom slugs

    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var url = requestContext.HttpContext.Request.Path.TrimStart('/');


        if (!string.IsNullOrEmpty(url))
        {
            if (url.Contains("_escaped_fragment_"))
                requestContext.HttpContext.Response.StatusCode = 401;
            else
                requestContext.HttpContext.Response.StatusCode = 404;

            FillRequest("Error","Index", requestContext);
        }

        return base.GetHttpHandler(requestContext);
    }

With this way, I can even implement 404 custom error page, which I was struggling without any lucks earlier (the code below is not working, the above does) :

        // Add this code to handle non-existing urls
        routes.MapRoute(
            name: "404-PageNotFound",
            // This will handle any non-existing urls
            url: "{*url}",
            // "Shared" is the name of your error controller, and "Error" is the action/page
            // that handles all your custom errors
            defaults: new { controller = "Error", action = "Index" }
Community
  • 1
  • 1
NeedAnswers
  • 1,411
  • 3
  • 19
  • 43