0

I am trying to change the location of all WordPress media to a subdomain located above the site root ie, above public_html. I updated the wp-config.php settings to:

define('UPLOADS', 'https://cdn.mysite.com/media');

And updated the image URLs for existing posts using the PHPmyAdmin search replace tool, eg, all

  • mysite.com/wp-content/uploads/ URLs to
  • cdn.mysite.com/media/

I also updated the wp-options table, upload path and upload url path fields as below:

  • upload path /myserverroot/user/cdn/media
  • upload url path https://cdn.mysite.com/media/

However when I refresh the webpages I see all media is missing, and when I check the IMG tag HTML I see the src URL is prepended by the website's base URL as below:

img src="https:// mysite.com/https://cdn.mysite.com/media/2019/9/image.jpg"

Any ideas on what the cause could be? Many thanks.

7
  • 2
    The constant of UPLOADS is relative to the ABSPATH, which is the absolute path to the WordPress directory. wordpress.org/support/article/editing-wp-config-php/… Have you thought of making a symlink that will connect the two? Sep 16, 2019 at 17:58
  • Hi I'm not quite sure how to do that, my knowledge in the area is limited. However the one tip that i used was to ensure the line define('UPLOADS', 'https:// cdn.mysite.com/media'); is added before the line require_once( ABSPATH . 'wp-settings.php' );
    – tammix
    Sep 17, 2019 at 1:57
  • @JosuaMarcelC, remember that the UPLOADS constant is always relative to ABSPATH. However, using a value like images.josuamarcelc.com shouldn't cause the "i got this for all images "josuamarcelc.com/https://images.josua..." - so, are you sure you didn't actually add "https://" when you defined UPLOADS? Are you using WordPress v6.0 - if not, what version are you using? Have you tried deactivating all plugins and/or switching to a default theme like 2021? Did the same issue persist?
    – Sally CJ
    Jun 15, 2022 at 2:25
  • You could also try clearing your caches. And @tammix, what's the solution you ended up using back then?
    – Sally CJ
    Jun 15, 2022 at 2:32
  • @SallyCJ i still cannot remove the ABSPATH after clearing the cache Jun 17, 2022 at 11:59

3 Answers 3

0

This will not work the way you are thinking. Wordpress will try to upload attachments (images, pdfs, etc) to your server using the UPLOADS constant, this path is a file path, local to the server.


If you wish to use a CDN you can get a CDN service to do this (manage files and upload them remotely to their service) or possibly use a tool like this to rewrite URLs on the fly.

I can't say how good this plugin is but seems like this could do what you are looking for, upload your attachments to another (remote) server.

1
  • i wish to put on my own same server, but only the url different url with subdomain Jun 19, 2022 at 6:51
0

To change the path to the media file, you can change the path via hook or directly in the database. However, a solid path with WordPress technic is a simple plugin that fire the hook and change the path of the media files.

The entries are in the table _options, entries upload_path and upload_url_path. You can set in the database a new path or use the following code snippet to change them with the help of the hooks.

add_filter( 'pre_option_upload_path', function( $upload_path ) {
    return '/path/to/static';
});

add_filter( 'pre_option_upload_url_path', function( $upload_url_path ) {
    return 'http://static.example.org';
});

Existing content should search, replace so that all existing media files have the right path inside the content of each post/page in the database. You will find all content, include URLs/Paths to the media files in the table _posts.

0

Try this in a demo website first.

Leave the upload_path empty.

Change upload_url_path to your subdomain.

In phpmyadmin use the code below to update already uploaded images attached in posts. Change wp_posts with your database prefix and you domain names

UPDATE wp_posts SET post_content = replace(post_content, 'https://mydemodomain.gr/wp-content/uploads/', 'https://images.mydemodomain.gr/uploads/');

And lastly in htaccess

RedirectMatch 301 ^/wp-content/uploads/(.*)$ https://images.mydemodomain.gr/$1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.