Search code examples
wordpress.htaccessmod-rewriteqtranslate

RewriteCond issue with subdomains and paths in .htaccess


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:

  • mydomain.cat or www.mydomain.cat ---> ca.mydomain.com ---> Shows website in catalan
  • mydomain.es or www.mydomain.es ---> es.mydomain.com ---> Shows website in spanish
  • mydomain.com or www.mydomain.com ---> en.mydomain.com ---> Shows website in english

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?


Solution

  • 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.