I have three different domains pointing to the same WordPress's host instance depending on the language (managed with the qTranslate plugin):
RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.cat$
RewriteRule ^/?$ "http\:\/\/ca\.mydomain\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteRule ^/?$ "http\:\/\/en\.mydomain\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.es$
RewriteRule ^/?$ "http\:\/\/es\.mydomain\.com\/" [R=301,L]
With that I've managed to achieve the following redirects:
Now the trouble I have is that, for example, user types mydomain.cat/work it doesn't works fine, it goes to mydomain.com/work/ and the language part in the subdomain is missing. It would be perfect if, in that case, it redirects to ca.mydomain.com/work.
I've researched and tried to modify the three RewriteCond rules to "add" the additional content at the end of the url (don't know if is better with QUERY_STRING or with parameters) if it exists, but I couldn't get it working fine, rather I don't know if it's the best way to achieve it.
Anyone can help me or give me a clue?
Ok, I got it working:
RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.cat$
RewriteRule (.*) "http\:\/\/ca\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteRule (.*) "http\:\/\/en\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.es$
RewriteRule (.*) "http\:\/\/es\.mydomain\.com\/$1" [R=301,L]
I added the $1 at the end of the RewriteRule statements and overwrote the ^/?$ at the beginning with (.*). I also added these two lines above the others:
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
To prevent the trailing slash. I hope this is helpful for somebody.