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

  • It's unclear what you are trying to achieve. What _should_ happen when you request `example.com/test/image.png`? Do you simply want to ignore the preceding URL-path? – MrWhite Jul 24 '17 at 12:57
  • Yeah @user82217, the preceding URL path should be ignored – George Tsatsis Jul 24 '17 at 12:58
  • I'm curious, what are you trying to capture with the trailing `(.*)` on the `RewriteRule` _pattern_? – MrWhite Jul 24 '17 at 13:09
  • @user82217 I run a service which serves files from a backend, I have hardcoded filetypes to be scanned as to what is served and what is not – George Tsatsis Jul 24 '17 at 13:11
  • Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D Jul 25 '17 at 16:57

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
  • 11,643
  • 4
  • 25
  • 40