3

I need to configure subdomains on my website for multilanguage support.
I can set up the domains for local use pointing to a folder but my host does not allow me to point them to the main root where my app is

/public/es
/public/www/index.php

es.domain.com needs to point to /public/www/index.php

I can't even use symlinks in the /es/ folder.

They replied to me

For what concerns your hosting pack, we could suggest you to use the .htaccess and mod_rewrite tools. You could place an .htaccess file in your subdirectories to rewrite the es.domain.com URL to domain.com/es/anything_else

In this way the visitor or search engine will still see

es.domain.com/anything as address.

I tried this is the /es/ folder but get a 403

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^es.domain.com/?$   ^domain.com/es_ES?$ [L,R=301]   
</IfModule>

EDIT My site is set up to change the language using the es.domain.com subdomain.

FFish
  • 10,964
  • 34
  • 95
  • 136
  • I've read your question but I didn't understand. So correct me if I'm wrong. you have `es.domain.com` and you want all requests to it redirected to `domain.com`? right? – undone Mar 11 '12 at 14:07
  • hi, no not redirected, rewritten. My host don't let me map subdomain to my main app in the www folder. – FFish Mar 11 '12 at 14:36
  • Sorry, I meant rewrite:-). So, for example `http://es.domain.com/index.php` goes to `http://domain.com/es_ES/index.php`? right? – undone Mar 11 '12 at 15:13
  • `es.domain.com` needs to point to `/public/www/index.php` my app figures out the language from the subdomain port – FFish Mar 12 '12 at 06:47
  • is `/public/www/index.php` document root of your main domain? – undone Mar 12 '12 at 08:22

2 Answers2

7

Here's what you may want to do, to be able to handle in the future many sub languages.

Check your host: if it begins with es, then change your document root. Here's the tip:

RewriteCond %{HTTP_HOST} ^es\.mydomain\.com$
# Change the path:
RewriteRule ^/?$ /public/www/index.php

And now something a little bit more advanced you may think about: multilanguage for the same website.

RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
# Create an environment variable to remember the language:
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]

# Now check if the LANGUAGE is empty (= doesn't) exists
RewriteCond %{ENV:LANGUAGE} ^$
# If so, create the default language (=es):
RewriteRule (.*) - [QSA,E=LANGUAGE:es]

Ok now we have an environment variable where the language is set.

You asked for this:

es.domain.com needs to point to /public/www/index.php

So add this final rule:

RewriteCond %{ENV:LANGUAGE} ^es$
# Change the root folder:
RewriteRule ^/?$ /public/www/index.php

So all in all:

RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
# Create an environment variable to remember the language:
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
# Now check if the LANGUAGE is empty (= doesn't exist)
RewriteCond %{ENV:LANGUAGE} ^$
# If so, create the default language (=es):
RewriteRule (.*) - [QSA,E=LANGUAGE:es]
# Change the root folder of the spanish language:
RewriteCond %{ENV:LANGUAGE} ^es$
# Change the root folder:
RewriteRule ^/?$ /public/www/index.php

What I don't get is: why can't you just write once for all languages, and make something like:

RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
# Create an environment variable to remember the language:
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
# Now check if the LANGUAGE is empty (= doesn't exist)
RewriteCond %{ENV:LANGUAGE} ^$
# If so, create the default language (=es):
RewriteRule (.*) - [QSA,E=LANGUAGE:es]

# WHATEVER THE LANGUAGE ADD IT TO THE URI:
RewriteRule (.*) $1?language=%{ENV:LANGUAGE} [QSA]

So now imagine someone types:

  • http://es.mydomain.com/
  • http://us.mydomain.com/
  • http://fr.mydomain.com/
  • http://pt.mydomain.com/

All point to the same piece of code and you just have to handle it in your Php file: look into the $_GET['language'] variable and read the good "translation" file.

This is just an advice to help you making less work for a very robust application!


Hope this helps!


[Edit 1]

Here's the final stuff you may put into your .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
    RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
    RewriteCond %{ENV:LANGUAGE} ^$
    RewriteRule (.*) - [QSA,E=LANGUAGE:es]
    RewriteRule (.*) $1?language=%{ENV:LANGUAGE} [QSA]
</IfModule>

[Edit 2]

My latest rules do this:

  • Original URL => where it goes
  • http://pt.domain.com/ => http://pt.domain.com/?language=es
  • http://pt.domain.com/aa.php => http://pt.domain.com/aa.php?language=es
  • http://es.domain.com/ => http://es.domain.com/?language=es
  • http://es.domain.com/aa.php => http://es.domain.com/aa.php?language=es
  • http://domain.com/ => http://domain.com/?language=es
  • http://domain.com/bb.php => http://domain.com/bb.php?language=es

It doesn't change the path at all.

This one changes the path:

RewriteCond %{HTTP_HOST} ^(us|fr|pt)\.mydomain\.com$
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
RewriteCond %{ENV:LANGUAGE} ^$
RewriteRule (.*) - [QSA,E=LANGUAGE:es]
RewriteCond %{ENV:LANGUAGE} ^es$
RewriteRule ^/?$ /public/www/index.php

So this should give:

  • Original URL => where it goes
  • http://pt.domain.com/ => http://pt.domain.com/
  • http://pt.domain.com/aa.php => http://pt.domain.com/aa.php
  • http://es.domain.com/ => /public/www/index.php
  • http://es.domain.com/aa.php => http://es.domain.com/aa.php
  • http://domain.com/ => /public/www/index.php
  • http://domain.com/bb.php => http://domain.com/bb.php
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • Wow thanks Oliver :-) I will check it out tomorrow. These rules go into the sub folders right? For each language. Your last code block is to put in the /www/ folder where my app is, right? – FFish Mar 09 '12 at 19:24
  • I've edited my answer so that you get, at the end, the full rewrite rule you just copy/paste to your `.htaccess` file. – Olivier Pons Mar 09 '12 at 21:24
  • Hi I just checked it out but I get a 403, Forbidden. I also wanted note that I updated my CMS to a version where it figures out localization by the subdomain, so it's not necessary anymore to link to `domain.com/es_ES/` anymore. `es.domain.com` should just point to `/public/www/index.php` – FFish Mar 10 '12 at 08:38
  • When I try your first code block `RewriteCond %{HTTP_HOST} ^(us|fr|pt|es)\.mydomain\.com$ RewriteRule (.*) - [QSA,E=LANGUAGE:%1] RewriteCond %{ENV:LANGUAGE} !^$ RewriteRule (.*) - [QSA,E=LANGUAGE:es] RewriteCond %{ENV:LANGUAGE} ^es$ RewriteRule ^/?$ /public/www/index.php` I get a 404, Not Found – FFish Mar 10 '12 at 08:43
  • Are you sure that `/public/www/index.php` exists? – Olivier Pons Mar 11 '12 at 20:38
  • Hi Oliver, yes that's the content when visiting domain.com Looks like the .htaccess in the /es/ folder can't make the jump to the /www/ folder. – FFish Mar 11 '12 at 20:50
  • I'm sorry but it seems I don't get the whole stuff. I'm editing my answer so that you can see (at the end of my answer) how, maybe, you could explain what works, what doesn't, and why it doesn't i.e. how it should work. I've edited and corrected a mistake (test if empty = `^$` not `!^$`) !! – Olivier Pons Mar 12 '12 at 09:47
  • Use `RewriteCond %{HTTP_HOST} (us|fr|pt)\.mydomain\.com$` to allow www.(us,fr,pt).mydomain.com as well. – aksu Nov 20 '15 at 15:58
2

try this

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^es\.
RewriteCond %{REQUEST_URI} !^/es
RewriteRule ^(.*)$   http://domain.com/es_ES/$1 [P,L]
Gerben
  • 16,747
  • 6
  • 37
  • 56
  • thanks for the reply, but I get a 404 "The requested URL /es_ES/ was not found on this server." I need to get to mydomain.com/es_ES/ – FFish Mar 03 '12 at 05:05
  • Also when I try `RewriteRule ^(.*)$ /albums/$1 [L]` I get a 500 - domain.com/albums/ is a valid URL in my `/public/www/` directory – FFish Mar 03 '12 at 09:32
  • Sorry. I assumed that the subdomain was in the same vhost. I missed that part in your question. I edited my solution above! – Gerben Mar 03 '12 at 13:08
  • Gerben, thanks so much for the help. Unfortunately it doesn't work yet.. I get a 404. Even hhen I do `RewriteRule ^(.*)$ http://domain.com/$1 [P,L]` I get a 400 "Your browser sent a request that this server could not understand. Size of a request header field exceeds server limit." – FFish Mar 03 '12 at 14:26