Matt Pierce
Forum Replies Created
-
UPDATE: This didn’t work when I went to production replica because our server admin has 2 virtual hosts (one for HTTPS and one for HTTP). On the upside I did figure out a working solution.
It turns out that Apache completely mangles URLs passed to mod_rewrite if the RewriteRule is in a Directory block that matches to a wildcard or regular expression. If you match to a specific directory then it behaves as expected. So now my Apache configuration looks like this:
NameVirtualHost network-test.localhost.me <VirtualHost network-test.localhost.me> DocumentRoot "c:/Sites/wpn_network-test" ServerName network-test.localhost.me </VirtualHost> # WordPress network sites - each has to be defined separately. # WARNING - Using a regular expression Directory will cause unexpected behavior # in mod_rewrite. DO NOT consolidate these. <Directory "c:/Sites/wpn_network-test"> RewriteEngine On RewriteBase / # If the visitor requested index.php, just end here RewriteRule ^index\.php$ - [L] # add a trailing slash to /wp-admin RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ [R=301,L] # If the visitor requested an existing file or directory, serve # the asset like normal RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] # If the visitor requested a resource in one of the wordpress directories # from within a subsite, silently give them the one from the network # wordpress directory RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 [L] # If the visitor requested a PHP file in the subsite, silently route # request to the network's PHP file (license.php, wp-config.php, etc ...) RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 [L] # If all else fails, allow index.php to handle the error condition. RewriteRule . /index.php [L] </Directory>
This means a little more work for me when creating a new test site, but testing and production both work.
Hooray, I’ve got it but it took some time. The problem is that Apache’s mod_rewrite module works differently depending on if it’s invoked from .htaccess, httpd.conf, and (importantly) a Directory statement within an httpd.conf file.
My solution here was to move the mod_rewrite commands out of the Directory block and into the VirtualHost block for my site. I lose the directory matching convenience, but hey – it’s working now. I also had to make some modifications because mod_rewrite from within a httpd.conf file expects leading slashes. Here’s the code (plus comments in case you ever wondered what all this stuff does).
<VirtualHost network-test.localhost.me> DocumentRoot "c:/Sites/wpn_network-test" ServerName network-test.localhost.me RewriteEngine On # If the visitor requested index.php, just end here RewriteRule ^index\.php$ - [L] # add a trailing slash to /wp-admin RewriteRule ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ [R=301,L] # If the visitor requested an existing file or directory, serve # the asset like normal RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] # If the visitor requested a resource in one of the wordpress directories # from within a subsite, silently give them the one from the network # wordpress directory RewriteRule ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 [L] # If the visitor requested a PHP file in the subsite, silently route # request to the network's PHP file (license.php, wp-config.php, etc ...) RewriteRule ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 [L] # If all else fails, allow index.php to handle the error condition. RewriteRule . /index.php [L] </VirtualHost>
That’s not how that works. Apache selects which one to use based on the name of the directory. The two don’t get used at the same time.