Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract text from url using Nginx

Tags:

nginx

I am trying to find a way to extract some text from a url to be used in alias. The url is something like:

/test/user_code_name/

and the alias should be

/test1/user_name/

So the idea is to just remove the part between _ and _ and use it like a variable.

Thx L.

like image 628
doktoreas Avatar asked Mar 12 '26 07:03

doktoreas


1 Answers

try these settings:

location / {
   if ( $request_uri ~ ^/(.*)/(.*)_.*_(.*)/$ ) {
       rewrite ^ /$1/$2_$3/ break;
   }
}

Please read more about nginx rewrite module here

like image 162
Sergei Lomakov Avatar answered Mar 15 '26 01:03

Sergei Lomakov



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!