0

I built an image randomizer so that when I open a .php file in a URL that it displays the image with readfile($randomImage);. This works locally but when I upload it to the server it gets blocked by the firewall since we do not want to be able to load URLs like

https://www.example.com/wp-content/themes/THEME/randomImage.php

I want to load a .php script on a .jpg file. What needs to happen is that when I use the URL (for instance)

https://www.example.com/image.jpg

and when I load this image that it runs a PHP file like

https://www.example.com/randomizer.php

In other words, I want WordPress to think that it is loading a URL ending with .jpg but opens a .php file

Is this something that is realizable?

2 Answers 2

0

Try the following at the top of the .htaccess file, before the existing WordPress directives:

# Internally rewrite "/image.jpg" to "/randomizer.php"
RewriteRule ^image\.jpg$ randomizer.php [L]

This uses mod_rewrite to rewrite the URL. There is no need to repeat the RewriteEngine On directive (that occurs later in the file).

Any request for /image.jpg (in the document root) is internally/silently rewritten to your /randomizer.php PHP script (also in the document root - assuming that is where your .htaccess file is located, or where the RewriteBase directive points to).

I have a weird request maybe

This isn't weird at all. In fact, using URL-rewriting like this is probably preferable to linking directly to your PHP script. Although calling the URL random-image.jpg or something similar maybe a good idea.

This is actually pretty much identical to a question I answered yesterday on StackOverflow: https://stackoverflow.com/questions/63340667/htaccess-redirect-one-file-to-another-including-query-string

6
  • Does this mean that there must be a file like image.jpg? Or does it work without its existance
    – w.jelsma
    Aug 11, 2020 at 11:58
  • "there must be a file like image.jpg?" - absolutely not. In fact, if image.jpg did exist then it will not be accessible since all requests are sent to randomizer.php instead.
    – MrWhite
    Aug 11, 2020 at 12:28
  • And how does routing work with the RewriteRule? I mean how do I specify the location of my randomizer.php file?
    – w.jelsma
    Aug 11, 2020 at 13:24
  • You can specify a root-relative URL-path (or file-path) - less the slash prefix (optional) - as the 2nd argument to the RewriteRule directive. eg. If the target file is https://www.example.com/wp-content/themes/THEME/randomImage.php then specify wp-content/themes/THEME/randomImage.php in the RewriteRule directive. (However, this can depend on what other directives you have in your .htaccess file.)
    – MrWhite
    Aug 11, 2020 at 13:33
  • How did you get on with this?
    – MrWhite
    Aug 28, 2020 at 23:53
-1

I created a function named randomImage() that simply takes a random image from an array, this function gets called in with the function below:

function imageRedirect(){
// Get the page name from the URL
$page = $_SERVER["REQUEST_URI"];

// Make sure debug mode is disabled since the deprecation errors will ruin the photo content
if (WP_DEBUG) throw new Exception('Due to depreciation errors this photo can only be rendered with debug mode disabled!');

// If we're on the random image page
if ($page == RANDOM_IMAGE_URL) {

    // Fetch a random image and return the data
    $image = randomImage();

    header("Content-Type: image/jpeg");
    header("Content-Length: " . filesize($image));
    readfile($image);

    exit;
}
}

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.