2

I am doing the SEO-optimization of my site and currently working with URLs. I've already removed the last slash and added redirection from

http://example.org to http://www.example.org.

Now I want to remove all extra-slashes from my URL: This URL :

www.exaple/about///graduation

should redirect to

www.example/about/graduation.

I found similar questions in SO, but they seems to be asked in context of pure ASP.NET. Using System.Uri to remove redundant slash

Remove additional slashes from URL

How can I implement the same in MVC5?

Community
  • 1
  • 1
Iskander Raimbaev
  • 1,322
  • 2
  • 17
  • 35

1 Answers1

3

Use a Code-behind redirect in your Global.asax like this;

   protected void Application_BeginRequest(object sender, EventArgs e)
{
    string requestUrl = Request.ServerVariables["REQUEST_URI"];
    string rewriteUrl = Request.ServerVariables["UNENCODED_URL"];
    if (rewriteUrl.Contains("//") && !requestUrl.Contains("//"))
        Response.RedirectPermanent(requestUrl);
}

I got this code from This Post, I hope that's useful to you =]

Community
  • 1
  • 1
Captain_Custard
  • 1,308
  • 6
  • 21
  • 35
  • Don't use this code! The code above has a nasty bug. If you are trying to do an error redirect (e.g. by using the httpErrors config section) it will get fed in something like `/Error/NotFound?404;http://localhost:81/e/e/e/e`, see the `//` in `http://` and then redirect to the original url, causing an infinite redirect loop. – Martin Capodici Oct 19 '18 at 06:28
  • Then I would recommend you comment on the original post, where I got the code from – Captain_Custard Oct 19 '18 at 06:32
  • I did also comment on the original post. – Martin Capodici Oct 20 '18 at 07:54