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