One of my friends has a multi-site installation of the WordPress, and he had a problem with the additional sites.

When he wanted to upload images, they uploaded into the proper folder, but the uploader provide wrong URL for the images, and he was not able to use them. The wordpress created the URL for all sites based on the main settings, and provided the “/files/image.jpg” style URL for all of the uploaded files. This setting was worked fine in the case of the main site, because a rewrite rule in the .htaccess file, but not with the sub-sites, because the files of the subsite stored in another folder.

The uploader created the URL based on the main site settings, but uploaded the images into the subsite upload dir.

In multisite installation, the WordPress stores the images in the wp-content/blog.dir/files subdirectory. First you have to set up the subsite properly. Go to the site settings, and fill the following fields;

In this example the additional site is the second in the row,
so it use the blogs.dir/2/files folder to store images

 

Permalink Structure: /%postname%/

Upload Path: /htdocs/wp-content/blogs.dir/2/files/

Upload Url Path: http://mysite.com/wp-content/blogs.dir/2/files/

Fileupload URL: http://mysite.com/wp-content/blogs.dir/2/files/

With these settings, the subsite will create the URLs of the uploaded content based on the settings, but we need one more modification.

There is a rewrite rule in the .htaccess, what change the “files/” style links to the appropriate  loader.

#RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

But when we want to use the uploaded files of the subsite, we don’t want to use this way, we need to use the original “blogs.dir/2/files” folder. To score this goal, we need to make some changes on the functions.php what are in the wp-includes folder.

You need to go to the function wp_upload_dir and somewhere around the line 2170, you have to do the following changes;

if ( is_multisite() && !$main_override && ( !isset( $switched ) || $switched === false ) )
{

// if isn’t subsite than change the upload dir
if ( defined( ‘BLOGUPLOADDIR’ ) )
$dir = untrailingslashit(BLOGUPLOADDIR);
if ( strpos($url, “blogs.dir/2″)  == 0)
{

$url = str_replace( UPLOADS, ‘files’, $url );

}

}

With this modification, the funciton leave the subsite upload URL in it’s original format, and won’t change it to “files/images.jpg”.

If  you have more sites, you can add all of them to the exclusion.

 

Related site: www.ontheglobe.com