Several people have asked for a feature which will enable the translation script to use subdomains (es.example.com) instead of subdirectories (www.example.com/es/).

Implementing this is not simple. The difficulties are mostly related to configuring your web server and DNS for subdomains. In comparison, configuring the script is easy.

I just made these changes on another one of my domains and used that change process to create this documentation.

Configure Apache for Subdomains

The first step is to configure Apache for subdomains.

The instructions for this are site specific.

Here's what I did.

I replaced this section in /usr/local/etc/apache22/extras/httpd-vhosts:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot /usr/local/www/data/example.com
ServerName http://www.example.com
ServerAlias example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com(:80)?$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
ErrorLog logs/example.com-error_log
CustomLog logs/example.com-access_log combined
</VirtualHost>

Here's what I replaced it with:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot /usr/local/www/data/example.com
ServerName http://www.example.com
ServerAlias example.com *.example.com
RewriteEngine on
ErrorLog logs/example.com-error_log
CustomLog logs/example.com-access_log combined
</VirtualHost>

This removed the automatic redirect from example.com to www.example.com, but we'll add that back in a moment.

Configuring DNS for Subdomains

The next step is to configure your DNS for subdomains.

Once again, the steps to accomplish this are very site specific.

We're running BIND here, so I added these records to the zone file for this domain:

en IN A 123.123.123.123
de IN A 123.123.123.123
es IN A 123.123.123.123
fr IN A 123.123.123.123
it IN A 123.123.123.123
pt IN A 123.123.123.123
ru IN A 123.123.123.123
nl IN A 123.123.123.123
el IN A 123.123.123.123
hi IN A 123.123.123.123
ja IN A 123.123.123.123
ko IN A 123.123.123.123
zh-cn IN A 123.123.123.123
zh-tw IN A 123.123.123.123
ar IN A 123.123.123.123

Edit /.htaccess

Next we will add three sections of code to the domains main .htaccess file.

This first section sends visitors to the right language, based upon the subdomain they use. It is also where we now block directories and file extensions which we don't wish to translate.
# Hostname based language selection
RewriteCond %{HTTP_HOST} !^www.*
RewriteCond %{REQUEST_URI} !^/includes/.*
RewriteCond %{REQUEST_URI} !^/images/.*
RewriteCond %{REQUEST_URI} !\.(exe|gz|zip|rar|asp|js|jsp|php|pdf|xls|mp3|avi| mpg|mpeg|wmv|gif|png|jpg|bmp|css|ico|GIF|JPG|PNG|f eed|trackback)$
RewriteRule ^(.*)$ /translate/index.php?page=$1 [L]

This section adds 301 redirects from the old subdirectory URL's to the new subdomain URL's.

# Redirects for v2 translation URLs
Redirect 301 /lang/en/ http://www.example.com/
Redirect 301 /lang/de/ http://de.example.com/
Redirect 301 /lang/es/ http://es.example.com/
Redirect 301 /lang/fr/ http://fr.example.com/
Redirect 301 /lang/it/ http://it.example.com/
Redirect 301 /lang/pt/ http://pt.example.com/
Redirect 301 /lang/ru/ http://ru.example.com/
Redirect 301 /lang/nl/ http://nl.example.com/
Redirect 301 /lang/el/ http://el.example.com/
Redirect 301 /lang/ja/ http://ja.example.com/
Redirect 301 /lang/ko/ http://ko.example.com/
Redirect 301 /lang/zh-TW/ http://zh-TW.example.com/
Redirect 301 /lang/zh-CN/ http://zh-CN.example.com/
Redirect 301 /lang/ar/ http://ar.example.com/

The third section replaces the non-www to www.redirect that I removed from httpd-vhosts and also adds a redirect for en (English) to www.

# Redirect to www.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^en.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Edit the Calls to the Translation Script

I was calling the translation script like this:
Code:
<a href="/lang/es<?php echo $_SERVER["REQUEST_URI"] ?>">Spanish<img src="/images/es.png" width="16" height="11" alt="Spanish"></a>
I edited these calls to use subdomains, like this:
Code:
<a href="http://es.example.com<?php echo $_SERVER["REQUEST_URI"] ?>">Spanish<img src="/images/es.png" width="16" height="11" alt="Spanish"></a></td>