I recently migrated a standard WordPress site to a WordPress multisite installation and was surprised to find that there’s no easy way to properly re-direct requests to old uploads once the folder structure is updated.
Here’s a simple .htaccess rule that I came up with to handle this:
RewriteCond %{HTTP_HOST} ^(app\.)?
RewriteRule ^content/uploads/([0-9]{4})/([0-9]{2})/(.*) /content/uploads/sites/3/$1/$2/$3 [R=301,L]
In the above snippet we’re creating a rewrite condition which states that the following rewrite rule should only apply to requests that are made on our subdomain, which is “app”. The website shown at our “app” subdomain is the site that we migrated from a standard WordPress install into a multisite installation, and so “app” now has a site ID of 3.
For the rewrite rule itself, we’re creating a 301 redirect for all requests made to the original date-based upload folder structure. These requests are redirected over to the new multisite folder structure which contains the site ID in addition to the date. Here is an example of this:
Old URL before redirect:http://app.domain.com/content/uploads/2017/01/photo.jpg
New URL after redirect:http://app.domain.com/content/uploads/sites/3/2017/01/photo.jpg
Now if a link somewhere on the web still points to the old uploads file structure for our WordPress site at app.domain then the request will redirect appropriately instead of resulting in a 404.
Hope this helps someone!
Leave a Comment