1

How would it be possible to have a wildcard directory proxy?

I have gotten the basics but the issue is, it gives out the directory which I'm in, my htaccess file:

RewriteEngine on

RewriteCond %{REQUEST_URI}  !(u|api|fonts|images|css|js) [NC]

# No directory

RewriteRule ^(.*)\.(jpg|gif|png|txt|mp4|mp3|json|js|zip|bmp|tiff|webp|webm|raw|psd|jpeg|jpe|wav)(.*) http://127.0.0.1:9000/owoapi/$1.$2$3?%{QUERY_STRING} [proxy]

# One Directory
RewriteRule ^([^/]+)/(.*)\.
(jpg|gif|png|txt|mp4|mp3|json|js|zip|bmp|tiff|webp|webm|raw|psd|jpeg|jpe|wav)
(.*) http://127.0.0.1:9000/owoapi/$1.$2$3?%{QUERY_STRING} [proxy]

When visiting domain.tld/image.png, it works just fine.

When visiting domain.tld/test/image.png, it 404's due to the fact that it includes /test/ in the proxied URL, how would I go about fixing that?

Edit: What I'm trying to achieve is to not include the preceding URL and make it act like it's being accessed via domain.tld/image.png instead of domain.tld/test/image.png

1 Answers1

0
# One Directory
RewriteRule ^([^/]+)/(.*)\. ......

Simply remove the braces around the first captured pattern (since you don't need to capture this group):

RewriteRule ^[^/]+/(.*)\. ......

Note that this only handles one subdirectory segment (which I assume is the intention).

Alternatively, you could have changed the backreferences from $1.$2$3 to $2.$3$4.

UPDATE#1: In addition to the above, you will need to change the preceding "No directory" block so that it indeed matches no directory. As it stands, it is matching the entire URL-path (ie. directories) and so the second rule is not even being processed...

# No directory
RewriteRule ^(.*)\.(jpg|gif| ......

Change to:

RewriteRule ^([^/]+)\.(jpg|gif| ......

UPDATE#2: To handle (and ignore) any level of subdirectory, simply remove the start of string anchor (ie. ^) from the above RewriteRule pattern. For example:

RewriteRule ([^/]+)\.(jpg|gif| ......
MrWhite
  • 13,315