Home > Software engineering >  htaccess redirect everything from subfolder to another domain
htaccess redirect everything from subfolder to another domain

Time:10-12

I'll redirect everything from my subfolder domain.com/zh to another domain like domain-china.com/zh but this isn't working ... my attempts were:

RewriteEngine On    
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com/zh/$ [NC]
RewriteRule .* https://domain-china.com/zh/$0 [L,NE,R=301]

or

RewriteEngine On
RewriteCond %{HTTP_HOST} domain\.com/zh$ [NC]
RewriteRule ^(.*)$ https://www.domain-china.com/zh/$1 [L,R=301]

I'm also need everything from querystring, f.e. domain.com/zh/product-xyz should rewrite into domain-china.com/zh/product-xyz and my folder /zh is only a virtural folder from my TYPO3 CMS (default language: domain.com; english: domain.com/en; chinese domain.com/zh; ...). There's no subfolders in my file structure at webserver.

CodePudding user response:

Variable HTTP_HOST only matches host name not the URI. For that you can use pattern in RewriteRule like this:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^zh/.*$ https://domain-china.com/$0 [L,NC,NE,R=301]
  • Related