How to redirect if user direct access image by browser?

I’m trying to make if a user is trying to direct access any image file by browser it should redirect to where the image is originally embedded but it isn’t working cause once it is redirected the image that is embedded on the page becomes broken. An example image link https://fabulous.pictures/LcOsfv7.jpeg should redirect to https://fabulous.pictures/LcOsfv7 but the embedded image becomes broken.

Here is what my page rule setting looks like

Any fix to this? also I want to only allow social media site to hotlink my images

Based on the information provided you would need to start by rewriting the specifications for how HTML works and then get every browser manufacturer to adopt it.

There simply isn’t data available in a web request available to support the behavior you are asking for.

Untested, but something like this might just work.

Additionally, you will need a redirect rule, similar to the one you already posted, however with a URL of https://domain/*-redirect.

Keep in mind, this does require all clients to send a referrer. If they don’t, your images will show for “regular” requests exactly as broken as they do now.

@ sandro Where do I put these settings in? it doesn’t look like it’s from page rules

https://yandex.com/search/?text=rewrite+path+to+cloudflare

@sandro I tried to deploy the rules but Cloudflare says:
Filter parsing error (1:40): regex_replace(http.request.uri.path, "\.jpeg$", "-redirect") ^ expected ", xHH or OOO after \

You probably need \\.

@sandro regex is only available for business & enterprise :frowning:

In that case that won’t work. You could use concat to append -redirect but you would still need to handle it server-side.

What may work is to use concat and then define the page rule for https://domain/*.jpeg-redirect, but that would really require a page rule for each image type.

You best handle this on your server.

Check out https://developers.cloudflare.com/ruleset-engine/rules-language/functions/ for the function.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.