Remote Address Rewrite

I use this rewrite in .htaccess to rewrite missing files like images. Is this possible with Caddy?

RewriteBase /wp-content/uploads/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$1 [L,P]

How does an internal rewrite work if the image is on a different site? :thinking:

You can use redir though!

Thanks for the reply. Was just basing this off what I used in my .htaccess.

So with redir can I redirect all files under a path that aren’t found locally to a remote file?

Welcome by the way!

No, redir simply maps a path to a redirect URL. (0.9 adds an if keyword you can use, but I’m not sure it supports whether files exist. @abiosoft could confirm, maybe get inspired… ;))

1 Like

Thanks! Been using Caddy along with Laravel Valet. Works great!

It doesn’t look like redir will work in this case. If rewrite allowed external destinations I would be able to pull this off. This would be useful when working on a website locally and you don’t want to pull GBs of images. Or if you are migrating a website to a different location and need time to move uploads from one location to another without impacting user experience with missing images.

On another note, think I found a working solution using without on proxy. (Which by the way has a missing description in the docs).

rewrite /wp-content/uploads/ {
    ext .jpg
    to  {path} {path}/ /rewrite/{uri}

proxy /rewrite/ {
    without /rewrite
1 Like

That’s clever!

And it is described in the docs, as prefix is a variable/placeholder value, which has a description of what the actual value should be:

prefix is a URL prefix to trim before proxying the request upstream. A request to /api/foo without /api, for example, will result in a proxy request to /foo.

Gotcha. Thanks for the help!

I am already considering depreciating ext for rewrite. Looks like it’s still useful.

The workaround would be to use regex or if match.

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