Home > OS >  Redirect domain to the new one with language subfolder
Redirect domain to the new one with language subfolder

Time:11-24

I having a problem with redirection of my website. A few days ago I bought a .com domain. There are two languages: Italian and German. German website was set on example.de and English version on example.it.

Now I changed it to .com domain: example.com with subfolders: example.com/it/ and example.com/it/.

How to redirect all links from example.it to example.com/it/ and example.de to example.com/de/.

...and following subfolders.

I was trying with

Options  FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.de$ [NC]
RewriteRule ^(.*)$ https://example.com/de/ [R=301,L]

But without success.

CodePudding user response:

RewriteCond %{HTTP_HOST} ^example\.de$ [NC]
RewriteRule ^(.*)$ https://example.com/de/ [R=301,L]

Having captured the URL-path in the RewriteRule pattern, you are missing the corresponding backreference in the substitution string, so this will redirect everything to the homepage, ie. example.de/<url> to example.com/de/ - the <url> is lost.

However, you can handle both TLDs with a single rule. Since the TLD is the same as the subdirectory.

I'm assuming all three domains resolve to the same place.

Try the following instead in the root .htaccess file:

RewriteCond %{HTTP_HOST} ^example\.(de|it) [NC]
RewriteRule (.*) https://example.com/%1/$1 [R=301,L]

The %1 backreference refers to the captured TLD from the preceding CondPattern. And the $1 backreference contains the URL-path captured in the RewriteRule pattern.

The RewriteBase directive is not required here.

Test with a 302 (temporary) redirect to avoid caching issues. You will need to clear your browser cache before testing, since the erroneous 301 (permanent) redirect will have been cached.


UPDATE#1: This is Polylang plugin for Wordpress.

In which case, this redirect directive must go at the very top of your .htaccess file, before any existing WordPress directives, including before the # BEGIN WordPress comment marker.


UPDATE#2: I also have .cz domain and subfolder is /cs (ISO standard)

Since the TLD and subdirectory are different, you will need to add another rule to handle this. (This can go before or after the above rule, since there is no conflict.)

For example:

RewriteCond %{HTTP_HOST} ^example\.cz [NC]
RewriteRule (.*) https://example.com/cs/$1 [R=301,L]
  • Related