• Hello everyone.

    I make use of WP Offload S3 Lite so all my media uploads from day 1 has been to amazon s3.

    I recently noticed that my posts are saved to my database using the main media url mydomain.com/wp-content/uploads but when displaying on website or editor the plugin changes the url to my cloudfront url using database search and replace queries once every media it uploaded is being called, This makes plenty of queries to my database on each page load.

    Since i don’t make use of my domain uploads url from the start i would like to know the best way to change my media url from mydomain.com/wp-content/uploads to foobarfoobar.cloudfront.net/wp-content/uploads without querying my database.

    if i find this then i can switch off the rewrite url option in the plugin which uses database.

    thanks

    • This topic was modified 8 years, 11 months ago by evilprogrammer. Reason: typo.. am new :)
Viewing 6 replies - 1 through 6 (of 6 total)
  • Caveat: I don’t know that specific plugin, but if the url after /uploads is exactly the same, then:
    – You could create a relatively simple rewrite in .htaccess to catch these. This is very simple, creates very small load on your server, since the redirect will be served even before WordPress would have started loading. But the visitor will still have one extra round-trip to your server before the content may be requested from the server.
    – You could add a filter to the hook when a post is saved to the database. Neatest solution: makes sure that you immediately serve the correct URL, without any additional filtering. But acts only upon saving. You’d need to either re-save all posts and pages, or do a search&replace on your database for all previous entries. (Or catch older entries through the .htaccess rewrite above)

    If needed, I can try and find the rewrite command for you, but you should be able to find what you need, there are several resources on this, some better than others. Just make sure that the rewrite is marked as L (last).

    Thread Starter evilprogrammer

    (@evilprogrammer)

    Thanks.

    Can i get an example of the .htaccess file and can htaccess also add a referrer to my redirect? cloudfront is set to allow requests from my domain.

    Thanks

    Referrer is a value in a http-request that the visiting browser may or may not include.

    Try to include the following in your .htaccess file. The sooner, the better, but it must be before the WordPress part:

    RewriteEngine on
    RedirectMatch 301 ^/wp-content/uploads(.*) https://foobarfoobar.cloudfront.net/wp-content/uploads$1

    The .htaccess file is construed by the webserver Apache. This rewrite command will send a 301 redirect (moved permanently) to the address in the last part of the command for any request that matches the first part of the request, copying the part in parenthesis into the position of $1

    Thread Starter evilprogrammer

    (@evilprogrammer)

    Thanks for this..
    Once it solves my issue i would mark this as resolved.

    Sure. Take your time.
    Still, I’d suggest that you consider finding a way to directly include the correct cdn address in your posts. This will allow visitors to have their pages load a little bit quicker, since they won’t need to first contact your server for every included image.

    Thread Starter evilprogrammer

    (@evilprogrammer)

    Worked but denied permission by amazon firewall which validates with referrer value.

    if there is any way you know of kindly direct me. i have search every where but cannot come up with one.

    I was actually thinking there is a function or CONSTANT that defines uploads url in wordpress or if there is a way i can do a php search and replace on the function that returns post content.. any ideas??

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘How do i change wordpress media uploads url to my cloudfront cdn url’ is closed to new replies.