Make WordPress Core

Opened 14 months ago

Last modified 14 months ago

#40465 new defect (bug)

Multisite fails to rewrite upload path on main site

Reported by: daniel berhane Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.3
Component: Networks and Sites Keywords:
Focuses: multisite Cc:


This is a follow-up to #12002.

After removing the /blog/ slug On options-permalink.php, the media filed display the wordpress subdirectory on the urls.


Install wordpress in its own directory Activate multisite with subsites Switch one of thd subsite into becoming main site Everything works smoothly including images

However, image urls contain wordpress directory.


Instead of


Is this the way it is designed to work? Or a bug? Thanks.

Change History (5)

#1 @johnjamesjacoby
14 months ago

Hi Daniel, thanks for taking the time, and congratulations on your first ticket!

I have a few questions I'd like to ask, to clarify this report and help researchers narrow the cause.

Where are you seeing image URLs not updating?

  • in posts contents?
  • when editing images?
  • Do galleries of images work?

Do you have database access, and are you able to see if the database has the correct values?

Off the top of my head, it's plausible that primary-site reassignment isn't directly exposed in the UI for exactly this reason, but it's been a while since I've needed to do this.

You can probably use a WP CLI find/replace to correct the database values, but you'll also want to disable and purge any cached data that might be associated with your installation afterwards.

A very succinct step-by-step to duplicate the problem (with VVV or some other common installation type) is also useful. If you have any additional information, please include it in your reply.

Thanks and congrats again!

#2 @swissspidy
14 months ago

#40466 was marked as a duplicate.

#3 @daniel berhane
14 months ago

Thanks John. I will try to be as detailed as possible.

Note: The site was installed as multisite from the beginning, with WP version 3.7 and in its own directory. It is WPMU with subsites, not subdomain.

Steps taken to switch subsite2 to a main site (since last week):

1/ Changed Site Address (URL) in /wp-admin/network/site-info.php of the main site and subsite2

subsite1 domain.com/new/
subsite2 domain.com/

2/ Changed blog id in wp-config.php.

define('BLOG_ID_CURRENT_SITE', 2);

3/ Search and replace urls in database Conducted search and replace using wp-cli:

wp search-replace 'domain.com' 'domain.com/new' wp_posts wp_postmeta --skip-columns=guid
wp search-replace 'domain.com/subsite2' 'domain.com' wp_2_posts wp_2_postmeta --skip-columns=guid

4/ Sql dump The next day I noticed the image gallery is broken (images not visible inside gallery). The images don't load along the post, just their caption. But when you click on the link embedded in the captions and open in a new tab, the image loads. So I created sql dump using Search & Replace plugin by Inpsyde GmbH to make the following replacements:

Replace 'http://domain.com/wp-content/uploads/sites/2/' with 'http://domain.com/wp-content/uploads/') in wp_2_posts post_content and wp_2_postmeta.

The image issue wasn't resolved.

5/ Removed blog slug All along, the /blog/ slug wasn't appearing on links. Then, I figured siteurl in wp_options and wp_2_options needs to be manually adjusted. Thus, I changed like this:

wp_options siteurl domain.com/new
wp_2_options siteurl domain.com/subdirectory

6/All posts of main site turned into 404. I can see the posts inside Posts Screen, but when I click the frontend /blog/ is automatically inserted/forced, thus leading to 404. So, taking a hint from ticket #12002, I checked on /site-settings.php permalink structure. Both subsite1 and subsite2 were set to /blog/%year%/%monthnum%/%day%/%postname%/ I didn't check subsite1 until that point, but subsite2 permalink structure was set to /%year%/%monthnum%/%day%/%postname%/ until I changed wp_2_options siteurl on step 5. Since that ticket says it's okay to remove /blog/, i removed it on site-settings.php

7/The posts of the main site became available at domain.com/%year%/%monthnum%/%day%/%postname%/ Until that time, subsite1(old main site) was acting as a clone of subsite2(new main site), listing all the posts of the later. Even if it was deactivated and archived. But now, it started listing only its own posts.

8/ Comparing the database with the backed-up database, I figured that at one of the earlier steps I should not have changed some urls that begin with domain.com/subdirectory/.... I also figured some urls that begin with domain.com/subsite2/ should have been changed to domain.com/subdirectory/..... rather than to domain.com. The urls point to plugin, wp-admin, etc. I fixed those using the interconnectit Search-Replace script.

9/ Moving image files I had the webhost staff copy the folders of public_html/subdirectory/wp-content/uploads/sites/2/ to public_html/subdirectory/wp-content/uploads/. Now, image gallery started displaying images.

10/ After two days, I noticed problem in other subsites' image galleries. All images were broken. Perhaps, the problem started earlier. But sometime around step 5 or 6, the file urls of those subsites were transformed to /subsite3/files/yyyy/mm/image-title.jpg. I guessed the time based on 404 reports log. A folder named blogs.dir was automatically created. Even if I deleted it, it's automatically created when image is added to a subsite.

So, I deleted all plugins and themes. And added a line into wp-config.php.

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
	define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
define( 'UPLOADS', 'wp-content/uploads' );

Now, everything is alright. Except the images of main site: Image gallery is okay. Post content okay. However, image url are set as: domain.com/subdirectory/uploads/yyyy/mm/image-title.jpg But they are also accessible at domain.com/ uploads/yyyy/mm/image-title.jpg Even if I stripped /subdirectory/ from the using the interconnectit script, they merely became custom urls of the images. File url is still domain.com/subdirectory/uploads/yyyy/mm/image-title.jpg on new image uploads.

I don't mind the subdirectory/ on image urls. But it seems a symptom of another problem. Plus, the images of the main site can be accessed through two urls. Not good.

Last edited 14 months ago by daniel berhane (previous) (diff)

#4 @daniel berhane
14 months ago

Update: I removed this line from wp-config.php two hours ago:

define( 'UPLOADS', 'wp-content/uploads' );

The site is working with no changes.

#5 @daniel berhane
14 months ago

I think my description was too long (I can shorten it if a moderator recommends so)

My hypothesis is that: The main site images were not properly configured at the time when I removed the /blog/ slug.

Therefore, even if the blog slug is now properly removed and the Rewrite_rules in wp_2_options is okay, the image library didn't learn that transformation because it was sort of disconnected at the time.

As a result, even if the main site image library is properly set now, it has somehow skipped that learning step, thus it is acting in disregard of the rewrite_rules.

Hence, it is using the siteurl as default image url. Still, it doesn't have any reason to deny image urls consisting only home url (without subdirectory), therefore, it works with those urls as well.

If my hypothesis makes sense, it is a bug. Because, in the absence of any code to the contrary, why would the main site ignore the rewrite_rules set in wp_2_options?

Note: See TracTickets for help on using tickets.