WordPress 3.0 Multisite Attachment Handling
One of the things that changes in WP 3.0 with multi-site is where file uploads and attachments get stored. This can be confusing to people trying to do export/imports and combine sites together.
In the .htaccess file of a multisite installation, you’ll find this little bit of code:
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
The effect of this code is to redirect requests that have “files” in the name to the ms-files.php. As you may have guessed, ms-files.php is the multi-site file handler.
Files in multi-site mode are stored in the /wp-content/blogs.dir structure. Inside that directory, you will find subdirectories labeled with numbers (1, 2, 3, etc.). Each number corresponds to the ID number of the individual site in the multi-site installation. Each one of these directories holds all the uploaded files for that installation. The ms-files.php file handles a bunch of caching parameters and then sends the file off to the browser upon request.
So the resulting URLs always look like http://example.com/files/2010/whatever.jpg and so forth. The “files” name is therefore special and reserved and cannot be used as a post slug and such.
When you export a site from somewhere and then import it into your new multi-site system, if you also choose to import the attachments, then you’ll have a minor problem. The attachments will be imported into this new files structure. However, the links from an older system are still referring to the old /wp-content/uploads/ directory structure that non-multi-site installs use. Therefore, you will need to go back through your posts and fix all these links and references to the attachments. I use the Search Regex plugin for this purpose, it works well enough and has some powerful search and replace capabilities.
Wouldn’t something along the lines of this work?
RewriteRule ^/wp-content/uploads/(.+) http://%{HTTP_HOST}/files/$1 [L]
Not with the [L] there, since that would stop processing. You could do it with an [R] to make it redirect, or just leave out the [*] entirely to allow the later rule to continue to modify things.
I always wondered what those letters meant, heh.
[R] would probably be best so the user ends up with the /files/ URL in their address bar.
Cool!!!
[…] View the original article here Esta entrada fue publicada en Sin categoría y etiquetada Attachment, Handling, Multisite, WordPress. Guarda el enlace permanente. ← ¡Hola mundo! […]
Looks like adding this line above the default rule works:
RewriteRule ^wp-content/uploads/(.+) http://%{HTTP_HOST}/wp-includes/ms-files.php?file=$1 [L]
Thanks for the post!
I am using a wordpress.org multisite.
When I use attachments (specifically eg attachments plugin) with multisite broadcast plugin, I get this. Do you know how to fix this?
Warning: copy() [function.copy]: The first argument to copy() function cannot be a directory in /var/www/MYDATABASENAME/data/www/pmndesign.com/wp-content/plugins/threewp-broadcast/ThreeWP_Broadcast.php on line 1824
and
Warning: Cannot modify header information – headers already sent by (output started at /var/www/MYDATABASENAME/data/www/pmndesign.com/wp-content/plugins/threewp-broadcast/ThreeWP_Broadcast.php:1824) in /var/www/MYDATABASENAME/data/www/pmndesign.com/wp-includes/pluggable.php on line 866
Hello!
I modified my website’s .htaccess file with your’s rows, but nothing changed. All of my pictures URLs still contains wp-content/uploads/sites/3/… tags. This method still works on WP 4.4.x versions?
Kind regards,
Jozsef