Redirection Based on Accept-Language Using ASP
ID: Q208935
|
The information in this article applies to:
-
Microsoft Internet Information Server 4.0
-
Microsoft Internet Information Services version 5.0
SUMMARY
When you run a multi-language site, you may want to redirect Web browsers to a directory containing content based on the language that is accepted by the Web browser.
MORE INFORMATION
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
Referer: <LINK TYPE="GENERIC" VALUE="http://www.microsoft.com/">http://www.microsoft.com/</LINK>
User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
Accept-Encoding: gzip, deflate
By testing for the Accept-Language value using Active Server Pages (ASP), a browser can be sent to a directory based on that value. The ASP code below detects the first accepted language code and redirects the Web browser to a folder with the same name as the accept language, or it does nothing if it is the default language for your site.
- Copy the following code, paste it into Notepad, and then save the file as "Langrdr.inc" (without the quotation marks) into 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
%>
- Copy the following two lines of code to the top of your Web site's Default.asp page:
<%@LANGUAGE="VBSCRIPT"%>
<!--#include virtual="/langrdr.inc"-->
- The above code may look similar to the following when 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>
- 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
For more information on the Accept-Language header and the currently-defined language codes, please see the following URLs:
ftp://ftp.isi.edu/in-notes/rfc1766.txt
http://www.dsv.su.se/~jpalme/ietf/language-codes.html
http://www.sci.usq.edu.au/staff/vance/iso639/
Additional query words:
Keywords :
Version : winnt:4.0,5.0
Platform : winnt
Issue type : kbhowto
|