Search code examples
apache.htaccessurl-rewritingdnssubdomain

Redirect URLs from many subdomains using .htaccess


I am changing my website structure and I need to redirect 301 many URLs from many subdomains. I have many subdomains, How can I do that in one .htaccess ? Redirect both :

ch.mydomain.com/coffee to www.mydomain.com/de_ch/coffee

it.mydomain.com/coffee to www,mydomain.com/it_IT/coffee

EDIT : Some URLs have folder and some have different name : exm :

ch.mydomain.com/coffetype/nespresso to www.mydomain.com/de_ch/nespressocafe

Thank you


Solution

  • You may use these redirect rules:

    RewriteEngine On
    
    # specific rules
    RewriteCond %{HTTP_HOST} ^ch\. [NC]
    RewriteRule ^coffetype/nespresso/?$ http://example.com/de_ch/nespressocafe [L,NC,R=301]
    
    # generic rules that have same URIs after redirect
    
    RewriteCond %{HTTP_HOST} ^ch\. [NC]
    RewriteRule ^ http://example.com/de_ch%{REQUEST_URI} [L,NE,R=301]
    
    RewriteCond %{HTTP_HOST} ^it\. [NC]
    RewriteRule ^ http://example.com/it_IT%{REQUEST_URI} [L,NE,R=301]