One of my frustrations with WordPress multisite is how ugly the upload directories get. I know, most people don’t see these upload URLs… but I do see them and they bother me. In our domain mapping post we described one approach to handling this. In that case we used a PHP function to get WordPress to look at a different directory. But another approach is to allow WordPress to use the directories it knows, but rewrite the URLs with Nginx. We had to do this recently for an older multisite network still using ms-files.php internally.
In this case we have multisite set up as a network using subdirectories for the subsites. Since the subdirectories are already being used for regular content, we thought that using /subsite/files would make sense for the upload directories. So an image’s URL might be https://hostname.org/subsite/files/2021/05/image.jpg.
We put the nginx rewrite rule into our /server/tenseg.conf file:
rewrite ^/([_0-9a-zA-Z-]+/)?files/(.+)$ /wp-includes/ms-files.php?file=$2 last;
This transforms a URL like https://site.org/subsite/files/image.jpg into https://site.org/wp-includes/ms-files.php?file=image.jpg.
Really, we might want to consider dumping ms-files altogether.