Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewritinghttp-status-code-301

Trying to obtain images from another location but RedirectMatch in htaccess is ignored


I've just setup a new subdomain 'preview', ie preview.example.com and I am setting up a replicate of the codebase of a live website (from the www. subdomain) so that I can have a duplicate environment for testing and showing my client recent changes.

I want to save myself having to copy over all the images to the preview and so the normal location under which images are saved in a WordPress site /wp-content/uploads/ is empty. I wanted to ues a simple htaccess rule to get these instead from the live subdomain where the image at the same path will exist. In other words, a request for

https://preview.example.com/wp-content/uploads/2021/01/test.jpg

would be redirected to the following where the image exists..

https://www.example.com/wp-content/uploads/2021/01/test.jpg

I've tried to set this up with the simplest of htaccess redirect rules but it just seems to be ignored and I've no idea why. Any idea how I can troubleshoot this?

RedirectMatch 301 /wp-content/uploads/(.*) http://www.example.com/wp-content/uploads/$1

Solution

  • Converting my comment to answer so that solution is easy to find for future visitors.

    Scenario is that both subdomain and main domains are on same host but their DocumentRoot are set to different paths. OP wants to serve lot of images from subdomain but doesn't to do bulk copy.

    Suggested approach without tinkering with .htaccess is to create a symbolic link of wp-content/uploads/. Only option suggested to use in .htaccess is:

    Options +FollowSymLinks
    

    Which allows use of symbolic links for serving web requests.