Home > Mobile >  Redirecting a WP multisite URL to a subsite URL
Redirecting a WP multisite URL to a subsite URL

Time:05-30

I have a WordPress multisite with a base URL of https://example.com. Subsites have an initial URL of https://example.com/forinstance, which can be changed in WordPress to an alias of https://forinstance.com.

But I want to redirect all URLs from https://example.com/caseinpoint ALSO to https://forinstance.com. There's not a way to do that in WordPress, so am trying to do it in my htaccess file. I can't get it to work though.

I've tried variants of:

RewriteCond %{HTTPS_HOST} ^example.com/caseinpoint$
RewriteRule (.*)$ https://forinstance.com [R=301,L]

But it always just goes to a 404 on example.com.

However, this DOES work for other subsites:

RewriteCond %{HTTPS_HOST} ^ejemplo.com$
RewriteRule (.*)$ https://perinstanza.com [R=301,L]

So it seems to be something to do with the multisite. How can I redirect example.com/forinstance, please?!

CodePudding user response:

Neither of the rules you posted will actually do anything since HTTPS_HOST is not a valid server variable. It should be HTTP_HOST, which represents the Host HTTP request header (hence the HTTP_ prefix). ie. the hostname (or domain) being requested.

The Host header does not include the URL-path, which you are trying to match in the first rule.

Presumably you want to redirect the remaining URL-path to the same on the new domain. eg. https://example.com/caseinpoint/<url> should redirect to https://forinstance.com/<url>? You aren't literally just redirecting https://example.com/caseinpoint (missing trailing slash) to https://forinstance.com/ (there is always a slash after the hostname) as you've stated?

In which case you can do something like the following at the top of the root .htaccess file, before any existing WordPress directives.

RewriteCond %{HTTP_HOST} ^(www\.)example\.com [NC]
RewriteRule ^caseinpoint(?:$|/(.*)) https://forinstance/$1 [R=302,L]

This also allows for an optional www subdomain.

The $1 backreference contains the value of the first capturing subpattern in the RewriteRule pattern, ie. the URL-path after "caseinpoint/" (not including the slash).

Test first with a 302 (temporary) redirect to avoid any potential caching issues. Only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed it works as intended. You will likely need to clear your browser cache before testing.

  • Related