简体   繁体   中英

Apache ProxyPass Based on Request URI Length

I've got a problem with Apache ProxyPass (more specifically ProxyPassMatch) where I'm trying to proxy https://domain.com/ {6 character key} to another server.

I've tried regex's like the following (attempting to take into consideration multiple ways that this might be handled by Apache):

ProxyPassMatch "/^.{22,22}$/g" https://domain.com/api/{6 character key}
ProxyPassMatch "/^.{7,7}$/g"   https://domain.com/api/{6 character key}
ProxyPassMatch "/^.{14,14}$/g" https://domain.com/api/{6 character key}
ProxyPassMatch "/^.{23,23}$/g" https://domain.com/api/{6 character key}
ProxyPassMatch "/^.{21,21}$/g" https://domain.com/api/{6 character key}
ProxyPassMatch "/^.{8,8}$/g"   https://domain.com/api/{6 character key}
ProxyPassMatch "/^.{6,6}$/g"   https://domain.com/api/{6 character key}
ProxyPassMatch "/^.{15,15}$/g" https://domain.com/api/{6 character key}
ProxyPassMatch "/^.{13,13}$/g" https://domain.com/api/{6 character key}

However nothing seems to work. Any help on this matter would be greatly appreciated.

The fix

You need to capture the characters in the pattern and then reference them in the url:

Example from the docs:

ProxyPassMatch "^/(.*\.gif)$" "http://backend.example.com/$1"

https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypassmatch

So in your case I think what you want is:

ProxyPassMatch "/^(.{6})$/"   "https://domain.com/api/$1"


Why/How this works

When using regular expressions, you can capture matched text by using brackets and then reference them using $1 for the first set of brackets, $2 for the second etc.

eg

   ProxyPassMatch "/^(.{6})/(.{6})$/"   "https://domain.com/api/$2/$1"

Would match http://domain.com/123456/ABCDEF and proxy to https://domain.com/api/ABCDEF/123456


One more thing

Also note you don't need {6,6} and can just use {6} to say you need the character to match exactly 6 times, you use this format where you want a variable number of characters eg {4,6} for between 4 and 6 - you can also specify {4,} for 4 or more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM