Media Folder Subdomain Creation

I was trying to create a subdomain for my pictures so I could increase site load speed. This is because your browser can load http requests from different subdomains in parallel, resulting in shorter load times. I read three separate articles from three separate domains that looked fairly reliable [1,2,3], and combined the information.

Summary

The TL;DR is that I failed, and I don’t know why. At the time of posting this, I didn’t really receive any help from anyone on this. It’s as if nobody does this, but I know that it’s a common thing to do. So, I am at a loss. I will still post my report on this so that perhaps others can take what I have learned and find the points of issue to complete a guide on the subject. First, some points:

  • I found a wpbeginner article talking about this, but it wasn’t very comprehensive. Also, it was 8 years old, and I can’t seem to find it
  • I will use https, replace if you have http. Also, replace media with your own subdomain name, and yourdomain with your own domain
  • I am new to web development, so I warmly welcome any corrections

STEP 1)

Set the subdomain up in your cPanel.

Let’s call it media.yourdomain.com

It should end up directly underneath/within the public_html hierarchy/folder. It may be worth checking the A records under Zone Editor to make sure it is registered. It probably will be.

STEP 2)

Change default media upload location.

There are two ways this can be done. If you’re particularly lazy, add the WP Original Media Path plugin and do it that way. Otherwise, head to yourdomain.com/wp-admin/options.php

ctrl+f -> upload_url_path -> type in https://media.yourdomain.com

Make sure to click the Save Changes button at the bottom of the page.

I’m not sure if filling out upload_path is necessary, but it’s interesting to note that once the upload_url_path is changed, the two options now become visible under Settings -> Media. So, I don’t really see the point in the above plugin.

STEP 3)

Move the files.

The second source omitted this. Download all the images/content from public_html/wp-content/uploads/ and then once done, upload it to the subdomain public_html/media/. Use an FTP client like FileZilla. Alternatively, you may be able to move it directly from within your hosting file manager.

STEP 4)

Update old image paths.

Go to cPanel -> phpMyAdmin -> Database Name. Enter:

UPDATE wp_posts SET post_content = REPLACE(post_content, ‘https://yourdomain.com/wp-content/uploads/’, ‘https://media.yourdomain.com/’);

Then click ‘GO’.

Note: Your table prefix might not be wp_. Check the left panel of phpMyAdmin. Also, I had to use double quotes instead of single quotes

For me, only 30 rows were affected… which seems like very few….

STEP 5)

Update image links in Media Library.

In the same place as the above step was performed, type in:

UPDATE wp_posts SET guid = REPLACE(guid, ‘https://yourdomain.com/wp-content/uploads/’, ‘https://media.yourdomain.com/’);

Then click ‘GO’.

For me, only 124 rows were affected. Again, this seems like very few as I had over 300 images.

I only found it in a single source. I don’t see why we would need to do this, as the Media upload location was changed in step 2, and the image paths were updated in step 4. I feel as this should happen automatically. A similar step is found in [5] but this link pertains to changing a website URL:

UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://yourdomain.com/wp-content/uploads/','https://media.yourdomain.com/');

My reasoning is that if the image links in the media library need to be replaced, shouldn’t the meta data links also be replaced?

STEP 6)

Redirect old image links to subdomain

I believe this is so people who may have, for example, bookmarked one of the images/media-content from your site, don’t get a 301 error message. Add to .htaccess file (which is in the root folder, and is a hidden file, enable view of hidden files):

RedirectMatch 301 ^/wp-content/uploads/(.*)$ https://media.yourdomain.com/$1

However, within another source [3], it says instead:

RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]

RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$

RewriteRule ^wp\-content\/uploads\/?(.*)$ "http\:\/\/subdomain\.yourdomain\.com\/$1" [R=301,L]

Which I think is basically the same thing… but I’m really not sure. Also, there are no details on where exactly within this file to add it. It seems like a rather important detail…

Done?

Step 7)

As for the last source [4], do we need to add to the wp-config.php file, before the line that reads require_once(ABSPATH.’wp-settings.php’); this below? (or is this basically what step 2 does?)

define(‘UPLOADS’, ‘https://media.yourdomain.com/uploads’);

Security

Last, I did not install WordPress on the subdomain. I’m not sure how security would work in that regard. I want my subdomain to be secure, so any suggestions on that would be appreciated.

Conclusion

So… it didn’t work. All the images on my site would not display. I restored to a day earlier from a backup that was automatically created. The thing is, I did steps 1-3 before that point, and if I used an earlier backup (1 week prior to that backup) I would end up losing a lot of work I did with my images – something I was absolutely not willing to do again. So, I did the restoration which was a little more involved than I thought it was [6] and then worked backwards from step 3. The only problem I noticed was that the website banner (Logo) was not displaying, even though it showed that it was set within the Media Library. So, I just did it in the customization.

The website looks fine, but I am a little shook up, and don’t know if everything IS in fact OK. I am confused as to why there is a complete lack of information regarding this. I have posted for help in the WordPress.org forums, yet to receive any help as of this post. I also messaged wpbeginner with this article to see if they are interested in creating a proper guide. Nidhi emailed me back to let me know they would let their “editorial team know”. My hosting support didn’t really give much help either.

So, here it is. My little adventure. Feel free to take what I have learned in creating your own complete guide on setting up an image subdomain after the fact of images. I’ve already let everyone I’ve contacted know that they can take this and use it. I don’t know if they will. It seems to me everyone is avoiding this topic and I don’t know why. Using a subdomain to take advantage of parallelized loading seems like a no brainer to me. Hopefully one day in the future I can get this set up.

Sources

Leave a Reply

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.