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?

1

1 Answer 1

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 =]

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Then I would recommend you comment on the original post, where I got the code from
I did also comment on the original post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.