Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulate or split string

I've a quick question, that would solve me some problems if possible:

Is it possible to split / manipulate the request-url with nginx?

What I mean is: an url like this: sub.somewhere.com/something/somethingelse

Is turned into:

subsomethingsomethingelse

And then further into:

sub/som/eth/ing/som/eth/ing/els/e

And then the given path is used to retrieve a File (so probably, it has to be stored in a variable that can be re-used, or used directly)

Is this possible somehow? Or if not, what exactly would be possible, and where are the limitations?

(edit) Are there native possibilities to do this, whitout including the PERL Module? Or is that the only way? (maybe a smaller module that only does string handling? )

like image 883
Katai Avatar asked Aug 07 '26 12:08

Katai


1 Answers

it is possible and relatively easy, all you need to do is match your location on a regexp with the approriate back references

location ~ (sub).(somewhere).(com)/(some)(thing)/(something)(else) {
  set $var1 = $1; # =sub in above example
  set $var2 = $2; # =somewhere in above example
  set $var3 = $3; # =com in above example
  set $var4 = $4; # =some in above example
  set $var5 = $5; # =thing in above example
  set $var6 = $6; # = something in above example
  set $var7 = $7; # = elsein above example
  rewrite ^ $1/$2 last; # would be sub/somewhere
}

you need to save the backreferences before the rewrite because the rewrite directive resets the references to those in the regexp first argument (so if you use some other directive like try_files that doesn't do that you coul just use the backreferences directly without saving them)

like image 129
cobaco Avatar answered Aug 09 '26 14:08

cobaco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!