0

Here's the situation:

Configuration

I have a WordPress multisite setup where different sites are on different domains. Some of those sites are contained in a subfolder in their domain, some are not. For the sites where they are in a subfolder, the subfolder name is the same across all sites.

Here's the examples:

  Site    |    URL
-----------------------
Main site | example.com
Site A    | sitea.com
Site B    | siteb.com/foo
Site C    | sitec.com/foo
Site D    | sited.com

Here is the relevant part of my wp-config.php:

/* Multisite */
define( 'WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
define('DOMAIN_CURRENT_SITE', 'example.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

Here is my .htaccess:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress

Problem

In this configuration, Main Site and Sites A & D work, but Sites B & C give a 500 error. If I change RewriteBase / to RewriteBase /foo/ then the reverse is true: Sites B & C work, but Main Site, Site A & site D give a 500 error.

If the effects of RewriteBase /foo/ were applied on a conditional basis only to domains where the URI started with /foo, then I think that would solve the problem. Unfortunately, I do not understand how WordPress and .htaccess interact well enough to do this.

Does anyone have any suggestions?

Thank you.

isUsername
  • 53
  • 7
  • Have you tried the domain mapping plugin (https://wordpress.org/plugins/wordpress-mu-domain-mapping) ? It's what I use – blurfus Mar 26 '15 at 19:56
  • I used to use it prior to WordPress having built-in support for multiple domains. I did re-install it based on your suggestion, however the issue still remains. – isUsername Mar 26 '15 at 20:12
  • Did you check this out? http://stackoverflow.com/questions/8621677/change-rewritebase-depending-on-domain – doublesharp Mar 26 '15 at 21:35
  • Or this http://serverfault.com/questions/237231/htaccess-with-conditional-rewritebase. Basically you can't conditionally set `RewriteBase` so you need to either put it into the `RewriteRule` as necessary (filtering using `RewriteCond`) or get fancy with using different `.htaccess` files per domain. – doublesharp Mar 26 '15 at 21:37
  • @doublesharp: Thanks. I used RewriteCond to basically do if/elses fior each WordPress RewriteRule, and that seems to be working. I posed the solution below. – isUsername Mar 27 '15 at 16:15
  • Cool - fwiw that's the route i would have gone as well. – doublesharp Mar 27 '15 at 17:45

1 Answers1

3

Found a solution for anyone who may stumble upon this. Basically, each part of a standard WordPress htaccess now has two RewriteConds: one for when it is in a subfolder, and another for when its not. Depending on that evaluation, the appropriate RewriteRule is applied.

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^/([_0-9a-zA-Z-]+/)wp-admin$
RewriteRule ^([_0-9a-zA-Z-]+/)wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_URI} !^/([_0-9a-zA-Z-]+/)wp-admin$
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]


RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]


RewriteCond %{REQUEST_URI} ^/([_0-9a-zA-Z-]+/)(wp-(content|admin|includes).*)
RewriteRule ^([_0-9a-zA-Z-]+/)(wp-(content|admin|includes).*) $2 [L]

RewriteCond %{REQUEST_URI} !^/([_0-9a-zA-Z-]+/)(wp-(content|admin|includes).*)
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]


RewriteCond %{REQUEST_URI} ^/([_0-9a-zA-Z-]+/)?(.*\.php)
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]

RewriteCond %{REQUEST_URI} !^/([_0-9a-zA-Z-]+/)?(.*\.php)
RewriteRule ^(.*\.php)$ $1 [L]


RewriteCond %{REQUEST_URI} ^/foo/
RewriteRule . foo/index.php [L]

RewriteCond %{REQUEST_URI} !^/foo/
RewriteRule . index.php [L]


# END WordPress
isUsername
  • 53
  • 7