I have a (Joomla) website using domain name foo.cz
.
This website has language mutations with the following structure:
foo.cz/music (the default CZ language has no specific directory)
foo.cz/en/music
foo.cz/fr/music
foo.cz/de/music
foo.cz/es/music
foo.cz/ru/music
Now I registered new domain bar.com
and I want to keep the .cz
website as it is (for the Czech users), but I need to do the permanent redirect from its language mutations to their equivalents on bar.com
, so redirects should be:
foo.cz/music -> do nothing, no redirect, stay where you are!!!
foo.cz/en/music -> bar.com/en/music
foo.cz/fr/music -> bar.com/fr/music
foo.cz/de/music -> bar.com/de/music
foo.cz/es/music -> bar.com/es/music
foo.cz/ru/music -> bar.com/ru/music
Please note that both domains are pointing to the same location, same document root on the server, so physically, I have still one site, one database, hosted at one exact place.
I want to do it using .htaccess
file, in some clever way to avoid some SEO and Google page rank damage. It means that .cz
default language site content will stay indexed as it is, and other language mutations will be reindexed under new domain bar.com
.
Both sites are running over https
.
I was trying something like this in .htaccess
:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.cz$ [NC]
RewriteRule ^ http://bar.com%{REQUEST_URI} [L,NE,R=301]
... but it didn't work ... I don't want to redirect everything, only the language mutations. So should I specify the language directories in RewriteCond
?. How? Or is there any better solution? Or am I thinking wrong and this is not simply possible because of the same hosting for both domains? Thanks so much for any help!
You have to make your pattern match starting language otherwise ^
will redirect everything.
You may use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?foo\.cz$ [NC]
RewriteRule ^[a-z]{2}/ http://bar.com%{REQUEST_URI} [L,NE,R=301]
[a-z]{2}/
will match any 2 lowercase letters followed by a /
.
If you only want to match certain specific languages then this should also work:
RewriteCond %{HTTP_HOST} ^(?:www\.)?foo\.cz$ [NC]
RewriteRule ^(?:en|fr|de|es|ru)/ http://bar.com%{REQUEST_URI} [L,NE,R=301]
Make sure to test this change in a new browser to avoid old cache.