.htaccess - Redirect domain/lang/ to subdomain for each language

I've been struggling with this for weeks and I can't get to the solution, I have to make a redirect in .htaccess of a multi-language website with all its pages following the same structure but with a subdomain for each language. In the following way that is valid for http as https:

domain.es/es/allpages  to  es.subdomain.com/allpages
domain.es/pt/allpages  to  pt.subdomain.com/allpages
domain.es/de/allpages  to  de.subdomain.com/allpages
domain.es/fr/allpages  to  fr.subdomain.com/allpages
domain.es/en/allpages  to  eu.subdomain.com/allpages

Any ideas? I have tried the following but not sure if it is correct:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) https://es.subdomain.com/%1 [R=301,NC]

RewriteCond %{REQUEST_URI} ^/pt/(.*)$
RewriteRule ^(.*) https://pt.subdomain.com/%1 [R=301,NC]

RewriteCond %{REQUEST_URI} ^/fr/(.*)$
RewriteRule ^(.*) https://fr.subdomain.com/%1 [R=301,NC]

RewriteCond %{REQUEST_URI} ^/de/(.*)$
RewriteRule ^(.*) https://de.subdomain.com/%1 [R=301,NC]

RewriteCond %{REQUEST_URI} ^/en/(.*)$
RewriteRule ^(.*) https://eu.subdomain.com/%1 [R=301,NC]

Thanks a lot!!!!

Upvotes: 2

Views: 173

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133508

Based on your shown samples, could you please try following. Please make sure you clear your browser cache before you test your URLs.

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.es$ [NC]
RewriteRule ^(es|pt|de|fr|en)/(.*)/?$ http://$1.subdomain.com/$2 [NE,R=301,NC,L]

Upvotes: 4

Related Questions