-2

I need to rewrite all requests to files at mydomain.com/storage/archive/ab7572d3-c697-4cfa-9b23-71b6f9c388d1

to

mydomain.com/public/storage/archive/ab7572d3-c697-4cfa-9b23-71b6f9c388d1

So I am adding "public" in the url, nothing else. The files are stored as shown above: 36 characters without extension. Therefore I am looking for a regex to match /storage/archive/ followed by 36 character long file name without extension

I am trying to make it through .htaccess file.

virrion
  • 416
  • 1
  • 5
  • 18
  • 2
    `[a-z0-9-]{36}` matches a 36 characters long combination of (lowercase) letters, digits and a minus "hyphen". – 04FS Nov 21 '19 at 07:17

1 Answers1

1

Use a RewriteRule to match the whole path and prepend your new parent to it:

RewriteEngine on
RewriteRule ^/(storage/archive/[0-9a-f-]{36}.*) /public/$1 [R]

The regex itself is pretty basic:

  • ^ match at the beginning of the path
  • / match a literal forward slash
  • (…) capture this parenthesized part as $1
    • storage/archive/ (literal text match)
    • [0-9a-f-]{36} match any [digit, letter from a to f, or a dash] 36 times
    • .* plus anything else you see (note, this will not include query strings)

(Yes, I could have put that first slash inside the capture, but I wanted it to be clearer when I rebuilt it with /public; otherwise, it'd be /public$1. Same functionality, but a little less legible.)

Adam Katz
  • 14,455
  • 5
  • 68
  • 83