Search code examples
.htaccesshttp-status-code-301

.htaccess 301 redirect to two different sites


Have a site source.com that is getting closed, so we would like to 301 redirect it to two other sites target1.com and target2.com.

Some specific urls on source.com must be 301 redirected to target1.com, including some wildcard urls like source.com/p/*

Other specific urls on source.com must be 301 redirected to target2.com, including some wildcard urls like source.com/q/*

Finally, all the urls that aren't covered by the lists/wildcards above must be redirected to the frontpage of target1.com

Is that possible to do via .htaccess?

Thanks!


Solution

  • You can use these rules in your site root .htaccess of source.com:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^(?:www\.)?source\.com$ [NC]
    RewriteRule ^p(/.*)?$ http://target1.com%{REQUEST_URI} [L,NC,R=301,NE]
    
    RewriteCond %{HTTP_HOST} ^(?:www\.)?source\.com$ [NC]
    RewriteRule ^q(/.*)?$ http://target2.com%{REQUEST_URI} [L,NC,R=301,NE]
    

    Even if you delete all content on source.com, you must keep the domain alive to make this redirection work.