URL rewrite in ASP.net for multiple languages

With an example URL:

www.domain.com/contact-us 

What is displayed in English . There are many other languages ​​supported by this site:

 www.domain.com/es/contact-us www.domain.com/jp/contact-us www.domain.com/de/contact-us www.domain.com/pt/contact-us 

Here is the rewrite rule for English (default language)

 <rewrite url="^/contact-us(\?(.+))?$" to="~/Pages/Contact.aspx$1" processing="stop"/> 

How do I change this / add a new rule for rewriting:

 www.domain.com/jp/contact-us 

To:

 ~/Pages/Contact.aspx?language=jp 

It is advisable without writing a new rule for each language for each page of content!

To complicate matters, IETF tags must be matched. They are diverse enough that it seems like a regular expression to match them would be a tricky way: https://en.wikipedia.org/wiki/IETF_language_tag

Ideally, I need to get a list of languages ​​from the database and combine the language tag field on the fly. But I'm not sure how to do this, since I only ever wrote static rules.

+4
source share
2 answers

Do you have access to the .htaccess file in your home directory? If so, you can make an automatic rewrite rule by writing the following

 Options +FollowSymLinks RewriteEngine on RewriteRule /(.*)/contact-us/ /Pages/Contact.aspx?language=$1 RewriteRule /contact-us/ /Pages/Contact.aspx?language=en 

Let me explain:

After the RewriteRule , the convenient URL on the left is /(.*)/contact-us/ , where (.*) Is where jp . Then jp is /Pages/Contact.aspx?language=$1 to the real URL on the right, here /Pages/Contact.aspx?language=$1 to replace $1 .

Input URL: http://www.example.com/jp/contact-us
Exit URL: http://www.example.com/Pages/Contact.aspx?language=jp

For the second RewriteRule , since the URL http://www.example.com/contact-us/ does not match the first model, it will automatically turn into http://www.example.com/Pages/Contact.aspx?language=en .

Remember that the .htaccess file MUST be in the root directory (next to the Pages folder). Otherwise, you will not get the desired result.

+1
source

Solved this by writing my own URL rewrite module. Sample code for anyone who encounters similar issues is shown below. I decided to reset all other URLs and redirect everything through this module.

Do not think that this is easily possible with static rules.

 public class DynamicURLRewrite : IHttpModule { public void Init(HttpApplication context) { context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest); } public void Dispose() { throw new NotImplementedException(); } void context_AuthorizeRequest(object sender, EventArgs e) { var rw = new Rewriter(); rw.Process(); } } public class Rewriter { public void Process() { using (MiniProfiler.Current.Step("Rewriter process")) { var context = HttpContext.Current; var rawURL = context.Request.RawUrl; var querystring = String.Empty; var urlParts = rawURL.Split('?'); var url = urlParts[0]; if (urlParts.Count() == 2) querystring = urlParts[1]; if (url.StartsWith("/")) url = url.Substring(1); // Get language component Translation.Language inLanguage = null; { foreach (var lang in Translation.Language.GetAllLanguages()) { if (url.StartsWith(lang.LanguageTag, StringComparison.CurrentCultureIgnoreCase)) { inLanguage = lang; url = url.Substring(lang.LanguageTag.Length); if (url.StartsWith("/")) url = url.Substring(1); break; } } if (inLanguage == null) { inLanguage = Translation.Language.GetLanguage( Settings.Translation.ProjectReferenceVersionRequiredLanguageTag); } } // Querystring { if (!String.IsNullOrEmpty(querystring)) querystring += "&"; querystring += "lang=" + inLanguage.LanguageTag.ToLower(); querystring = "?" + querystring; } // Root pages { if (String.IsNullOrEmpty(url)) { context.RewritePath("~/pages/default.aspx" + querystring); return; } if (url.Equals("login")) { context.RewritePath("~/pages/login.aspx" + querystring); return; } 

And then a few more complicated rules:

  // Handlers if (url.StartsWith("handlers/")) { // Translation serving if(url.StartsWith("handlers/translations/")) { var regex = new Regex("handlers/translations/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).json", RegexOptions.IgnoreCase); var match = regex.Match(url); if (match.Success) { context.RewritePath("~/handlers/translation/getprojecttranslation.ashx" + querystring + "&project=" + match.Groups[1] + "&language=" + match.Groups[2]); return; } } } 
+3
source

All Articles