.htaccess redirect based on language

I’m looking for a .htaccess pro.

I want to redirect from one domain to an other.
For example shop.domain1.com -> domain2.com/shop this is easy :wink:
But If I want to make an additional language parameter, it gets trickier:
shop.domain1.com/de -> domain2.com/de/shop

This almost works:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^shop\.domain1\.com$ [NC]
RewriteRule ^(.*) https://www.domain2.com/$1/shop/ [L,R]

But how can I make that / optional, if there is no language parameter? Because now the redirected link looks like: www.domain2//shop

Thanks for all hints and help

Rewriting isnt the same as a redirect. If i understand your goal, What you need to do is bounce from domain1 to domain2, not manipulate the urls in domain1.

Redirect 301 /de domain2.com/de/shop

Thanks for your help. That make sense. I couldn’t test it yet, but I will see if it works.