1

How does one implement URL redirecting for URLs with accents? I'd like all potential URL requests with accents to be rewritten without the accent. This is for client names on a French language site. Users should be typing the name without the accent, but should they not do so then I'd like them to land on the correct page either way.

Example

User enters: www.mysite.fr/client/andré ==> user is redirected towww.mysite.fr/client/andre

The resource identifier (clientName) in the database is stored without the accent. Here's my RouteConfig.cs :

        routes.MapRoute(
            name: "ClientDetails",
            url: "client/{clientName}",
            defaults: new { controller = "Client", action = "ClientDetails" }

I understand that there are various methods for removing accents on a string. And yes, this would allow me to remove accents from the received URL parameter within the Controller method. However, I would like to be consistent, so that users always see URLs displayed without accents. Therefore, how do I implement redirecting URLs throughout my web application? Is there an easy way to do this in RouteConfig.cs or Web.config ?

Community
  • 1
  • 1
full_prog_full
  • 1,139
  • 10
  • 17
  • Maybe you should prevent such urls in the first place? (Also not clear why your current routing has problem with accented characters). – Alexei Levenkov Dec 11 '15 at 19:50
  • 2
    Possible duplicate of [How can I remove accents on a string?](http://stackoverflow.com/questions/3769457/how-can-i-remove-accents-on-a-string) – MethodMan Dec 11 '15 at 19:54
  • @MethodMan Not exactly, as that question does not explain how to implement URL rewriting for accents. It only shows how to change a string. I'm missing that last step. Thanks – full_prog_full Dec 11 '15 at 20:17
  • @AlexeiLevenkov as stated above, the resource identifier (in database) does not allow accents. I agree that preventing URLs with accents is best practise. And this is why i'd like to rewrite any URLs with accents to the non-accentuated equivalent – full_prog_full Dec 11 '15 at 20:21

2 Answers2

3

I think you mean Redirect and not Rewrite given that Rewrite would mean that the URL stays the same, and you display the intended content (which I think is what you don't want).

The strategy that I think you want, can be implemented by creating a custom route constraint that matches everything that has an any special chars on it.

Make this custom route to be the first thing to be evaluated in your route table, and map this custom route to a "RedirectController" (that you will create) that takes care of removing the special chars from the URL and redirecting the user to a URL with no special chars

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • 1
    Correct, updated my question from `Rewrite` to `Redirect`. Does anyone have an implementation example for URLs with accents ? – full_prog_full Dec 11 '15 at 20:26
  • 2
    Google for "RegEx Diacritics" e.g. http://stackoverflow.com/questions/30247777/regex-diacritics-in-c-sharp-not-filtering-on-ismatch – C. Augusto Proiete Dec 11 '15 at 20:39
  • Wouldn't this require making 2 custom routes, one that contains any accents and one that contains no accents? – Pluto Dec 14 '15 at 17:54
0

At the beginning of every request, you can make this check to perform a redirect. In your Global.asax.cs file, include the following...

protected void Application_BeginRequest()
{
    var originalUri = HttpContext.Current.Request.Url;
    var finalUri = new UriBuilder(originalUri);

    finalUri.Path = RemoveAccents(
        originalUri.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped)
    );

    // Check if the URL has changed
    if (!originalUri.Equals(finalUri.Uri))
    {
        HttpContext.Current.Response.Redirect(finalUri.Uri.ToString(), true);
        HttpContext.Current.Response.End();
    }
}

You might also want to add another line for finalUri.Query and UriComponents.Query. For RemoveAccents, I tried the following code but you can use what you'd like:

public string RemoveAccents(string input)
{
    return Encoding.UTF8.GetString(Encoding.GetEncoding(1251).GetBytes(input));
}
Pluto
  • 2,900
  • 27
  • 38