How to Map a Domain with subdirectory to a Multi-site Network
-
NOTE: This is for expienced users…. sorry noobies you’re on your own.
How to map subdirectory blogs with Multi-stie Domain Mapping
In order to be able to correctly configure your server you will need ACCESS TO HTTPD.CONF on your Apache server. This configuration CANNOT be done with .htaccess
Main concept
This is for website managers that have an established site and wish to create a blog network that extends further than their single domain where the url structure of the target domain needs to have a subdirectory.
We achieve this by installing WP in a subdomain: blog-admin.mydomain.com and enable SUBDOMAIN networking.
Then using a combination of server ENVIRONMENTAL VARIABLES, ALIAS and modification of the MS/MU files in WP.
This requires PLANING, so work out exactly what you want before you start.WARNING: By modifying the httpd.conf (or site specific include as in PLESK) incorrectly file you can cause your web server to FAIL TO START. Furthermore you can cause your existing site to NOT FUNCTION CORRECTLY. That being said let’s have some fun.
For example Purposes let’s set up our domain structure:
Main site blog admin:
https://blog-admin.mysite.comBlogs (or any combination):
https://www.mysite1.com/en/blog/
https://www.mysite1.com/es/blog/
https://www.mysite1.com/fr/blog/
https://www.mysite2.com/ blog/
https://www.mysite3.com/blog/Testing Environment:
Windows 7 64bit
(Xampp custom installation)
Apache 2.2
Mysql 5.21
Php 5.3
Highly recommended that you add the following to monitor your rewrite:
RewriteLog [AbsolutePathToLogFile]/rewrite_log_blog
RewriteLogLevel 3The program mTail for windows is the same as tail in linux so you can monitor in real time all the rewrite steps.
Considerations for Production Servers:
Make sure that if you have php_open_basedir set you must include the ABSOLUTE PATH TO WP INSTALLATION DIRECTORYIf you have SymLinksIfOwnerMatch enabled such as PLESK, you will need to override this for your WP installation directory unless you install all the files with the same user
So, here we go!
Step One: Create directory structure
Create all the necessary folders. In this example case I’ve created:
[AbsoluteSitePath]/mysites/
[AbsoluteSitePath]/mysites/mysite1.com
[AbsoluteSitePath]/mysites/mysite1.com/blog-admin
[AbsoluteSitePath]/mysites/mysite2.com
[AbsoluteSitePath]/mysites/mysite3.comStep Two: Setup Webserver
Depending on what software you use you will need to make the necessary entries in your server configuration.
For testing purposes I’ve edited the /winidows/system32/drivers/etc/hosts file with the following in order to get the local DNS to handle the request:
127.0.0.1 blog-admin.mysite1.com
127.0.0.1 https://www.mysite1.com
127.0.0.1 mysite1.com
127.0.0.1 https://www.mysite2.com
127.0.0.1 mysite2.com
127.0.0.1 https://www.mysite3.com
127.0.0.1 mysite3.comMake the virtual hosts for each domain:
<VirtualHost mysite1.com:80>
ServerAlias https://www.mysite1.com
ServerName mysite1.com
…..
</VirtualHost/In EACH VirtualHost entry you will need to add the following (This is for testing, on a production server you should restrict the AllowOverride to something more secure:
<Directory “[AbsoluteSitePath]/mysites\mysite1.com\blog-admin”>
Order deny,allow
Deny from all
Allow from all
AllowOverride All
</Directory>
STEP 3: Install WordPress
Copy the wordpress files to [AbsoluteSitePath]/mysites/mysite1.com/blog-admin.
Install WP as per the instructions
Enable Multsite in the wp-config.php with define(‘WP_ALLOW_MULTISITE’, true);
Create the blogs.dir directory in wp-content.
Now STOP! The next steps are different that the standard MS installation.
Add the following the wp-config.php file replacing your WP domain instead of mine:
define( ‘MULTISITE’, true );
define( ‘SUBDOMAIN_INSTALL’, false );
//$base = ‘/’;
define( ‘DOMAIN_CURRENT_SITE’, ‘blog-admin.mysite1.com’ );
define( ‘PATH_CURRENT_SITE’, ‘/’ );
//define( ‘SITE_ID_CURRENT_SITE’, 1 );
//define( ‘BLOG_ID_CURRENT_SITE’, 1 );
Step 4: Create the blogs and install MU Domain mapping plugin
Note: there is a bug with eliminating a blog and then creating another with the SAME PATH. This is IMPORTANT because the PATH is variable that this tutorial depends on
I’ve now created the following blogs:
ATTENTION: Look at the pattern of the mysite1.com blogs. The in language “code”. This is important for the rewrite as the base is “mysite1blog” + “[LanguageCode]”Address: mysite1blogen
Title: My Site 1 Blog 1
Address: mysite1bloges
Title: My Site 1 Blog 2
Address: mysite1blogfr
Title: My Site 1 Blog 3
Address: mysite2blog
Title: My Site 2 Blog
Address: mysite3blog
Title: My Site 3 Blog 3
Important: Uncheck “Update site url and home as well” next to the path field
Now we must edit each one so that it has the correct SiteURL, PATH and HOME. For example we go to the sites-> edit page for mysite1blogen and put:
Domain: blog-admin.mysite1.com
PATH : /mysite1blogen/
SiteURL: https://www.mysite1.com/en/blog/
Home : https://www.mysite1.com/en/blog/
FileUpload URL : https://blog-admin.mysite1.com/mysitebogen/files
Domain: https://www.mysite2.com
PATH : /mysite2blog/
SiteURL: https://www.mysite2.com/blog/
Home : https://www.mysite2.com/blog/
FileUpload URL : https://www.mysite2.com/blog/files
Now install the MU domain mapping plugin and follow its instructions.
Add blog-admin.mysite1.com to the CNAME in the options page.
Map ONLY mysite2blog to mysite2.com and mysite3blog to mysite3.com. DO NOT MAP mysite1.com to anything.
STEP 5: Customizing the Rewrite Rules
Create a file called rewrite.wp-multi.include and save it to the apache “conf” folder.
Copy the following into it:
RewriteEngine On
RewriteRule ^index\.php$ – [L]
RewriteRule ^(/[_0-9a-zA-Z-/]+/) index.php [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [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]RewriteCond %{ENV:BLOG_BASE} ^(.*)$
RewriteRule . %1index.php [L]STEP 6: Installing and Configuring the Module Rewrite
Inside EACH <Directory “D:\Home\htdocs\checkout\adminsystem\www\mysites\mysite1.com\blog-admin”> Add the follow : Include conf/rewrite.wp-multi.include
Now, we must configure each VirtualHost .
VirtualHost mysite1.com
NOTE: As we are using a variable first “folder” in the blog url which in our case is the Language Code. If the patter is /blog/en/ for example we would not need to edit the httpd.conf file if we create more blogs.
Add the following inside the VirtualHost directive( substitute [AbsolutePathToHtdocs] for your server configuration:For Pattern /“LanguageCode”/blog/:
Alias /en/blog [AbsolutePathToHtdocs]/mysites/mysite1.com/blog-admin/
Alias /es/blog [AbsolutePathToHtdocs]/mysites/mysite1.com/blog-admin/
Alias /fr/blog [AbsolutePathToHtdocs]/mysites/mysite1.com/blog-admin/
Now add:
RewriteEngine On
# RewriteLog [PathToYourLogFile]
RewriteLogLevel 3
RewriteRule .* – [E=BLOG_PATH_BASE:mysite1blog]
RewriteCond %{ENV:BLOG_PATH_BASE} ^(.*)$
RewriteRule ^/([a-z]{2})/(blog/)(.*) – [E=REDIRECT_SCRIPT_URL:/%1$1/$3,E=REDIRECT_BLOG_PATH:/%1$1/,E=BLOG_PREFIX:$1,E=BLOG_BASE:/$1/blog/]
VirtualHost mysite2.com and VirtualHost mysite3.com
Now add:
RewriteEngine On
# RewriteLog [PathToYourLogFile]
RewriteLogLevel 3
RewriteRule ^/(blog/)(.*)$ – [E=BLOG_BASE:/blog/]Alias /blog D:/home/htdocs/checkout/adminsystem/www/wp-blogs/blogadmin/
STEP 7: Editing the WP PHP files
ATTENTION: Make a backup of all files listed below!
In this step we are going to modify the following files:
sunrise.php
ms-settings.php
ms-load.php
This can get a bit tricky.
The main concept is now to use the Environmental Variable REDIRECT_BLOG_PATH to correctly find our blogs in the virtual system of WordPressSunrise.php
After Line 19 Add:
// Detect if we are Redirecting our blog in a patterned sub-directory style
$domain_mapping_id = ($_SERVER[‘REDIRECT_BLOG_PATH’]) ? false : $domain_mapping_id;
Line 22 Replace with:
// If we are re-mapping from domain.com/directory structure
// $current_blog->path = ‘/’;
$current_blog->path = ($_SERVER[‘REDIRECT_SCRIPT_URL’]) ? $_SERVER[‘REDIRECT_SCRIPT_URL’] : ‘/’;
ms-load.php
After line 140 add:
// Check to see if we are coming from a pattern subdirectory structure
$path = ($_SERVER[‘REDIRECT_BLOG_PATH’]) ? $_SERVER[‘REDIRECT_BLOG_PATH’] : path;
After Line 163 Add:
// Check to see if we are coming from a pattern subdirectory structure
$path = ($_SERVER[‘REDIRECT_BLOG_PATH’]) ? $_SERVER[‘REDIRECT_BLOG_PATH’] : $path
Ms-settings.php
At line 50 Add:
// Check to see if we are coming from a pattern subdirectory structure
$path = ($_SERVER[‘REDIRECT_BLOG_PATH’]) ? $_SERVER[‘REDIRECT_BLOG_PATH’] : $path
Replace Line 53 ($current_blog = get_blog_details ….) with:
$current_blog = get_blog_details( array( ‘domain’ => $domain ,’path’=>($_SERVER[‘REDIRECT_BLOG_PATH’]) ? $_SERVER[‘REDIRECT_BLOG_PATH’] : false), false );
After Line 74 ($path .= $blogname . ‘/’;) Add:
$path =($_SERVER[‘REDIRECT_BLOG_PATH’]) ? $_SERVER[‘REDIRECT_BLOG_PATH’] : $path;
$domain = ($_SERVER[‘REDIRECT_BLOG_PATH’]) ? $current_site->domain : $domain;Ms-default-constants.php
At Line 30 Add:
// For use in plugins that upload files or need to access uploaded files
if(is_subdomain_install()):
$domain = $current_blog->domain.””.(($_SERVER[‘BLOG_BASE’]) ? $_SERVER[‘BLOG_BASE’] : “”);
else :
$domain = $_SERVER[‘HTTP_HOST’].””.(($_SERVER[‘BLOG_BASE’]) ? $_SERVER[‘BLOG_BASE’] : “”);
endif;define( ‘MS_CONTENT_URL’, “https://”.$domain.UPLOADS );
- The topic ‘How to Map a Domain with subdirectory to a Multi-site Network’ is closed to new replies.