• 0

If image doesnt exist on subdomain, redirect it to different domain


Question

Hey,

Im having some problems with a self hosted "CDN". Im using a wordpress plugin called W3 Total Cache to store images, css and js on a subdomain.

The problem is, some of the thumbnails that wordpress creates are not being uploaded to the cdn subdomain.

Is there anyway, using .htaccess, that i can redirect requests from cdn.mydomain.com/changing/path/to/image/picture.jpg to www.mydomain.com/changing/path/to/image/picture.jpg if that file does not exist on the cdn subdomain?

Thanks in advance

Mike

3 answers to this question

Recommended Posts

  • 0

could probably do a custom 404 page, and just see where the user was trying to go (how they got to the 404) and just parse it, and if they are looking for an image, just do a header location. or an html re-direct.

  • 0

Hi Firey,

Thanks for your reply.

Im not exactly sure what your suggesting there?

I basically just want it so that if the image doesnt exist on the subdomain it will still display the image from the origin domain.

I've been doing some googling and ive seen this example


RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|gif|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ /no_picture.png [L]
[/CODE]

but i dont really know how to alter this to suit my needs.

The request URL is going to be http://cdn.mydomain.co.uk/... and i need to change that to http://www.mydomain.co.uk/...

Thats the only part i want to change, i want the rest of the url to stay the same, as both domains have the same folder structure.

If someone could show me how to change this ,htaccess code, i would be very greatful.

In the mean time i'll keep googling the matter :D

Thanks in advance

  • 0

Hey,

Just in case anyone else has a similar problem to me, ive found a workable solution based on the code i posted before.

I placed this code on cdn.mydomain.co.uk.


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|gif|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . http://www.mydomain.co.uk/%{REQUEST_URI} [L]
</IfModule>
[/CODE]

This basically means that if the CDN subdomain gets a request for an image file that it doesn't have, it redirects the request to the main domain (which does have the image).

This topic is now closed to further replies.
  • Posts

    • This is listed as a preview in WU for me. I usually don't grab them as previews; what's your opinion on it so far?
    • Pale Moon 34.3.1 by Razvan Serea Pale Moon is an Open Source, Goanna-based web browser available for Microsoft Windows, Linux and Android, focusing on efficiency and ease of use. Make sure to get the most out of your browser! Pale Moon offers you a browsing experience in a browser completely built from its own, independently developed source that has been forked off from Firefox/Mozilla code, with carefully selected features and optimizations to improve the browsers speed, resource use, stability and user experience, while offering full customization and a growing collection of extensions and themes to make the browser truly your own. Features: Optimized for modern processors Based on proprietary optimized layout engine (Goanna) Safe: forked from mature Mozilla code and regularly updated Secure: Additional security features and security-aware development Supported by our user community, and fully non-profit Familiar, efficient, fully customizable interface Support for full themes: total freedom over any elements design Support for easily-created lightweight themes (skins) Smooth and speedy page drawing and script processing Increased stability: experience fewer browser crashes Support for many Firefox extensions Support for a growing number of Pale Moon exclusive extensions Extensive and growing support for HTML5 and CSS3 Many customization and configuration options Pale Moon 34.3.1 changelog: Pale Moon will now exclude local resources from CSP checks, aligning it with the rest of CSP handling. Fixed an issue where the devtools JSON viewer would, in some cases, make erroneous requests to remote servers. Updated libpng to 1.6.58+apng. Updated NSS to 3.90.12 (UXP), addressing multiple security issues. Fixed several intermittent and rare crashes. Security issues addressed: CVE-2026-12318 (CWE-125), CVE-2026-12322, CVE-2026-12292 (DiD), and multiple other issues that did not have a CVE designation at the time of patching. Download: Pale Moon (64-bit) | Portable 64-bit | ~40.0 MB (Freeware) Download: Pale Moon (32-bit) | Portable 32-bit Links: Pale Moon Homepage | Add-ons | Themes | Extensions | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Onwards to Windows 12 then lol.
  • Recent Achievements

    • Grand Master
      Jaybonaut went up a rank
      Grand Master
    • One Year In
      Philsl earned a badge
      One Year In
    • Dedicated
      Scoobystu earned a badge
      Dedicated
    • First Post
      Tom Schmidt earned a badge
      First Post
    • One Month Later
      D0nn13 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      425
    2. 2
      +Edouard
      177
    3. 3
      PsYcHoKiLLa
      124
    4. 4
      Michael Scrip
      77
    5. 5
      Xenon
      76
  • Tell a friend

    Love Neowin? Tell a friend!