How to perform redirection based on Accept-Language by using ASP (208935)



The information in this article applies to:

  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0

This article was previously published under Q208935
We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

SUMMARY

This step-by-step article describes how to redirect Web browsers based on multiple language capability by using Active Server Pages (ASP).

When you run a multi-language site, you may want to redirect Web browsers to a directory that contains content based on the language that is accepted by the Web browser.

When a Web browser makes a request to a Web server, it sends an HTTP request that may look similar to the following:
   Accept: application/vnd.ms-excel, */*
   Accept-Language: en-us,ru;q=0.5
   Connection: Keep-Alive
   Host: www.microsoft.com
   User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
   Accept-Encoding: gzip, deflate
				
By using ASP to test for the Accept-Language value, a browser can be sent to a directory based on that value. The sample ASP code in this article detects the first accepted language code and redirects the Web browser to a folder with the same name as the accepted language, or it does nothing if it is the default language for your site.

back to the top

ASP code for redirect


  1. Paste the following code in Notepad, and then save the file as Langrdr.inc in the root of your Web site:
          <%
         Dim strURL,strACCEPT
         strACCEPT = GetAcceptLanguage()
    
         ' Change the language code below to customize the default.
         If strACCEPT <> "en-us" Then
           strURL = "http://" & Request.ServerVariables("HTTP_HOST")
           strURL = strURL & "/" & strACCEPT & "/"
           Response.Redirect strURL
         End If
    
         ' This function returns the first accept-language.
         Function GetAcceptLanguage()
           Dim strHTTP, strLANG, strTEMP, intTEMP
           strHTTP = LCase(Request.ServerVariables("ALL_RAW"))
           If InStr(strHTTP,"accept-language:") Then
             strHTTP = Trim(Mid(strHTTP,16+InStr(strHTTP,"accept-language:")))
             For intTEMP = 1 to Len(strHTTP)
               strTEMP = Mid(strHTTP,intTEMP,1)
               If IsAlpha(strTEMP) Or strTEMP = "-" Then
                 strLANG = strLANG & strTEMP
               Else
                 GetAcceptLanguage = strLANG
                 Exit Function
               End If
             Next    
           End If
         End Function
    
         ' Determine if the character passed is a letter.
         Function IsAlpha(strCHAR)
           If (Asc(strCHAR)>=65 And Asc(strCHAR)<=90) Or _
             (Asc(strCHAR)>=97 And Asc(strCHAR)<=122) Then
             IsAlpha = -1
           Else
             IsAlpha = 0
           End If
         End Function
       %>
    					
  2. Paste the following two lines of code in the top of your Web site Default.asp page:
       <%@LANGUAGE="VBSCRIPT"%>
       <!--#include virtual="/langrdr.inc"-->
    					
    This code may look similar to the following when it is used in a Web page:
    <%@LANGUAGE="VBSCRIPT"%>
       <!--#include virtual="/langrdr.inc"-->
       <html>
       <head><title>Welcome</title></head>
       <body>
       <h1>Welcome to my page!</h1>
       </body>
       </html>
    					
  3. Add directories or virtual directories based on language codes. For example:
       de = German, c:\inetpub\wwwroot\de
       es = Spanish, c:\inetpub\wwwroot\es
       fr = French, c:\inetpub\wwwroot\fr
       ru = Russian, c:\inetpub\wwwroot\ru
    					
back to the top

REFERENCES

For more information about the Accept-Language header and the currently-defined language codes, visit the following Web sites:

Tags for the Identification of Languages
ftp://ftp.isi.edu/in-notes/rfc1766.txt

ISO 639 and IETF 1766 Standardised Language Codes
http://www.dsv.su.se/~jpalme/ietf/language-codes.html

back to the top

Modification Type:MinorLast Reviewed:5/30/2006
Keywords:kbCodeSnippet kbhowto kbHOWTOmaster kbLocalization kbScript KB208935