Iskander Raimbaev
Iskander Raimbaev

Reputation: 1362

Removing extra-slashes from Url in ASP.NET MVC5,

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?

Upvotes: 2

Views: 1495

Answers (1)

Colonel_Custard
Colonel_Custard

Reputation: 1390

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

Upvotes: 3

Related Questions