To redirect only the document root of the respective domains then you could do something like the following using mod_rewrite near the top of your .htaccess
file.
RewriteEngine On
# Redirect "example.com/" to "example.com/en"
RewriteCond %{HTTP_HOST} =example.com
RewriteRule ^$ /en [R=302,L]
# Redirect "example.de/" to "example.de/de"
RewriteCond %{HTTP_HOST} =example.de
RewriteRule ^$ /de [R=302,L]
This assumes you have already canonicalised the hostname (ie. no www
subdomain).
As per your example, I have omitted the trailing slash on the target URL-path (eg. /en
). However, if /en
(and /de
) are physical subdirectories then you must include the trailing slash, otherwise mod_dir will trigger a secondary 301 (permanent) redirect to append the trailing slash.
The regex ^$
- matches the document root only (an empty URL-path). The CondPattern =example.com
is an exact match string comparison (not a regex) that matches against the Host
HTTP request header.
Note also that this is a 302 (temporary) redirect. Language redirects generally should be "temporary", allowing the user to change the language. However, if this is intended to be permanent, then change it to a 301, but only once you have confirmed that it's working as intended.