Remove itok from uri to match image

1. Caddy version (caddy version): 2

2. How I run Caddy:

I’m running caddy in my localhost

a. System environment:

Arch linux

b. Command:

caddy start

d. My complete Caddyfile or JSON config:

localhost:1234 {
  root * mysite.com
  file_server

  uri replace \?itok=(.*)$ ""
}

3. The problem I’m having:

I’m flattening a drupal site to serve it with caddy. Drupal by default adds some random query strings to all the images. Something like mysite.com/sites/default/files/styles/large/public/image21.jpg?itok=L_byRGxn

I’m trying to figure out a way to rewrite the uri so cady can search for the image that exists in my filesystem but doesn’t include the ?itok=L_byRGxn part.

4. Error messages and/or full log output:

No errors

5. What I already tried:

I tried the uri replace \?itok=(.*)$ "" described in my Caddyfile without success.

Thanks in advance!

Actually after moving the

uri replace \?itok=(.*)$ ""

before

file_server

It worked! :partying_face:

1 Like

The file_server directive doesn’t take into account the query part of the URL when looking on disk, it only looks at the path. It would surprise me if your uri replace line does anything.

Also, the order in which directives are in a site block in the Caddyfile does not matter because they are sorted based on the preset directive order (except if you use a route block):

2 Likes

@francislavoie you are right. I tried this again and indeed, the uri didn’t change anything relevant.

The thing is I removed all the itok pars from my local files following the instructions provided here Creating a static archive of a Drupal site | Drupal.org and incorrectly though this fixed the problem.

Thanks for your help. It seems the path to go is just to remove the ?itok=21j2k1 tokens from the filesystem and Caddy will take care of the files without issues.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.