如何为每种语言创建子域?

I want to make website in multiple languages, I do it right with cookies to detect the user preferred language.

Now I want to make each language has its sub-domain to be indexed in search engines, some thing like Facebook.

so I want to configure my Apache to point all my virtual hosts to the same folder, then use $_SERVER["SERVER_NAME"] to figure out what sub-domain was requested.

I start to use this code in my htaccess file:

<VirtualHost *>
    ServerName mydomain.com
    ServerAlias en.mydomain.com ar.mydomain.com es.mydomain.com
</VirtualHost>

but it doesn't work and I get cannot find server when I request es.mydomain.come so any help ?

BTW I use mod_rewrite after that code above! and I didn't create any sub-domain in cPanel!

You can direct all subdomains to the same folder using a wildcard *

<VirtualHost *>
      ServerName mydomain.come
      ServerAlias *.mydomain.com
</VirtualHost>

As long as your CNAME records are fine as BluesRock said

try this:

<VirtualHost *:80>
  DocumentRoot /www/public //whatever the root is!
  ServerName domain.com
  ServerAlias en.domain.com, es.domain.com
</VirtualHost>

Another note to remember:

$_SERVER["SERVER_NAME"]//will always show domain.com
$_SERVER["HTTP_HOST"]//Will show en.domain.com if that is where they entered from.