Search code examples
wordpress.htaccesshttp-redirectmod-rewritemultisite

Wordpress Multisite Domain Redirects


I have a Wordpress multisite with a primary domain that is used for admin site management and then hundreds of other sites that each have their own unique domain (NOT a subfolder or subdomain of the primary site).

I need to redirect each site to its corresponding landing page on our main site. I am programmatically generating these redirect rules since there are hundreds of redirects that need to be set up. I am then adding those redirect rules to one master .htaccess file at the document root.

Here is what I have so far:

RewriteCond %{HTTP_HOST} ^(.*)?firstsite.com [NC]
Redirect 301 / https://newsite.com/1234/ 

RewriteCond %{HTTP_HOST} ^(.*)?secondsite.com [NC]
Redirect 301 / https://newsite.com/5678/

What I'm seeing is that it is redirecting all traffic from any of the sites to the first "Redirect 301" address (https://newsite.com/1234/). Presumably because I'm doing "Redirect 301 /" which is redirecting the document root of the entire Wordpress multisite instead of the root of the "RewriteCond" domain specified.

How do I set up essentially a conditional redirect based on the domain being accessed?

I should mention that I do not have Nginx installed on the server.


Solution

  • RewriteCond and RewriteRule work together. So the Redirect 301 was running independently of the RewriteCond. This will work:

    RewriteCond %{HTTP_HOST} ^(.*)?firstsite.com [NC]
    RewriteRule ^(.*)$ https://newsite.com/1234/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^(.*)?secondsite.com [NC]
    RewriteRule ^(.*)$ https://newsite.com/5678/$1 [R=301,L]
    

    Also, be sure to place this at the very top of your .htaccess rewrite rules. Right underneath the line that says:

    RewriteEngine On