Skip to content

Instantly share code, notes, and snippets.

@ifthenelse
Last active February 7, 2016 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifthenelse/dad6951b3eedc0808d45 to your computer and use it in GitHub Desktop.
Save ifthenelse/dad6951b3eedc0808d45 to your computer and use it in GitHub Desktop.
Redirect to the page which most suits the client's language settings. Choice is made by Apache server contextually to "Accept-Language" HTTP header values. Current example fallbacks to English page if no suitable language is found.
# Declare the file which contains the index of its traslations
DirectoryIndex index.html.var
AddHandler type-map .var
# Redirect to specific language by explicit URL request
# /en -> index.html
# /it -> index_it.html
# /es -> index_es.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^en$ index.html [L]
RewriteRule ^it$ index_it.html [L]
RewriteRule ^es$ index_es.html [L]
</IfModule>
# Decide which language page serve
<IfModule mod_negotiation.c>
LanguagePriority en it es
ForceLanguagePriority Fallback
</IfModule>

Simple Apache single page redirect.

├── .htaccess       (redirects the browser to a specific page whether it points to /en /es or /it urls)
├── index.html.var  (*must* be your DirectoryIndex)
├── index.html      (English page and language fallback page)
├── index_es.html   (Spanish page)
└── index_it.html   (Italian page)

Serves the page which most suits the client's language settings. Choice is made by Apache server contextually to "Accept-Language" HTTP header values. Current example fallbacks to English page if no suitable language is found.

Make sure you have mod_negotiation enabled with $ apachectl -M | grep negotiation or $ ls /etc/apache2/mods-enabled/negotiation.load

Tested on Apache 2.2 and 2.4

For more info:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>English page</title>
</head>
<body>
<h1>English and language-fallback page</h1>
</body>
</html>
URI: index.html
Content-language: en
Content-type: text/html
URI: index.html
Content-language: it
Content-type: text/html
URI: index_it.html
Content-language: es
Content-type: text/html
URI: index_es.html
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<title>P&aacute;gina en espa&ntilde;ol</title>
</head>
<body>
<h1>P&aacute;gina en espa&ntilde;ol</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="utf-8" />
<title>Pagina italiana</title>
</head>
<body>
<h1>Pagina italiana</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment