How to Move the Upload Directory to a Subdomain in WordPress
Moving media files to a subdomain is a common speed optimization strategy.
Background
Each time we upload a file to the Media Library, WordPress creates a new attachment and stores the file in the upload directory. By default, this directory is at wp-content/uploads.
Website owners sometimes move the upload directory to a subdomain. A common reason is to improve page load time without using a CDN, as browsers download resources from different servers in parallel.
Notice
This tutorial is about moving the upload directory to a directory outside the WordPress directory, like a subdomain directory. If you need to set the upload directory to a directory within the WordPress directory, I explain an easier way here.
Important: Back up your website before applying the procedure, so you can revert changes easily if something goes wrong.
Steps
Follow this procedure to move the upload directory to a subdomain directory. It also works for addon domains.
Step 1: Navigate to Settings > General.
Step 2: In the URL bar, replace options-general.php with options.php. The URL is now like https://example.com/wp-admin/options.php. Press Enter. The All Settings screen is shown.
Step 3: Look for the option 'upload_path'
and set its value to the absolute path of the subdomain directory.
Step 4: Look for the option 'upload_url_path'
and set its value to the URL of the subdomain directory.
Step 5: Scroll down and click Save Changes.
Step 6: If your website was already storing media files, move the contents of the previous upload directory to the subdomain directory.
Step 7: Upload a sample file and check that it is stored in the new location.
Step 8: Navigate through your website and verify that images are displayed correctly, files are downloaded correctly, etc.
Issues
If you get an error while uploading a file, log into cPanel and verify that the subdomain directory is writable. The recommended permission is 755
.
If you discover that some URLs are still pointing to the old upload directory, use a search and replace operation to update these obsolete URLs in your content.
You may also need to add a rule to the .htaccess file, so URLs previously indexed by search engines are redirected to the new location.
# Rule required if media files are moved from 'wp-content/uploads' to 'https://photos.example.com'
RedirectMatch 301 ^/wp-content/uploads/(.*)$ https://photos.example.com/$1
Further reading
I recommend the other tutorials in this series to learn more about managing attachments in WordPress.
- Big Image Handling in WordPress
- How to Restrict the Maximum Upload Size in WordPress
- How to Increase the Maximum Upload Size in WordPress
- How to Change the Upload Directory in WordPress
- How to Move the Upload Directory to a Subdomain in WordPress
- How to Force File Download in WordPress
- How to Disable Attachment Pages in WordPress
- How to Customize the URL of Attachments in WordPress
Source code
The source code developed in this tutorial is available here.
Comments