# Best way to get the $request\_uri without the starting slash

**URL:** <https://www.ruby-forum.com/t/best-way-to-get-the-request-uri-without-the-starting-slash/215003>\
**Category:** NGINX\
**Created:** [December 19, 2011, 10:55am UTC](https://www.ruby-forum.com/t/best-way-to-get-the-request-uri-without-the-starting-slash/215003 "2011-12-19T10:55:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [December 19, 2011, 10:55am UTC](https://www.ruby-forum.com/t/best-way-to-get-the-request-uri-without-the-starting-slash/215003/1 "2011-12-19T10:55:26Z")

</div>

I want to pass the $request\_uri to memcached as the key but without the  
starting slash, as our memcached key names are a prefixed integer which  
can be referenced from the URI, as follows:

[http://example.com/1000](http://example.com/1000) → prefix:1000

In nginx, the only (or all) variables that refer to the URI also contain  
the starting slash. So if I were to use prefix:$request\_uri as above I  
would get prefix:/1000 as the key.

At the moment I’m solving this by using a regex conditional to set a key  
name (this is paraphrased):

if ($request\_uri ~\* ^/(.\*)$) {  
set $request\_key $1;  
}

set $memcached\_key prefix:$request\_key;

So now I have the correct key name for memcached. Are there any better  
ways of doing this? Would this have a serious impact on performance,  
performing this conditional every time? Is it possible do this using set  
without the conditional part?

Thanks for any help,  
Matt.

---

<div class="post-metadata">

**Author:** ![Matthew\_Ward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matthew\_Ward](https://www.ruby-forum.com/u/Matthew_Ward)\
**Post date:** [December 19, 2011, 12:08pm UTC](https://www.ruby-forum.com/t/best-way-to-get-the-request-uri-without-the-starting-slash/215003/2 "2011-12-19T12:08:29Z")

</div>

On Monday 19 December 2011 13:54:33 Matthew Ward wrote:  
[…]

> So now I have the correct key name for memcached. Are there any better ways  
> of doing this? Would this have a serious impact on performance, performing  
> this conditional every time? Is it possible do this using set without the  
> conditional part?

You can utilize “map” module functionality for such tasks, e. g.:

map $request\_uri $request\_key {  
default index;  
~^/(.+)$ $1;  
}

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Matthew\_Ward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matthew\_Ward](https://www.ruby-forum.com/u/Matthew_Ward)\
**Post date:** [December 20, 2011, 10:37am UTC](https://www.ruby-forum.com/t/best-way-to-get-the-request-uri-without-the-starting-slash/215003/3 "2011-12-20T10:37:34Z")

</div>

Thanks, that’s exactly what I was looking for.

The $1 didn’t work however, nginx complained about it being an  
unreferenced variable, so I modified it to look like the examples in the  
documentation (maybe you can’t reference pattern matches numerically  
like elsewhere in the config?):

map $request\_uri $request\_key {  
default index;  
~^/(?P.+)$ $key;  
}

Posted at Nginx Forum:

> **[Re: Best way to get the $request\_uri without the starting slash](https://forum.nginx.org/read.php?2,220245,220299#msg-220299)**
>
> I want to pass the $request\_uri to memcached as the key but without the starting slash, as our memcached key names are a prefixed integer which can be referenced from the URI, as follows: http://example.com/1000 -\> prefix:1000 In nginx, the only...

---

<div class="post-metadata">

**Author:** ![Matthew\_Ward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matthew\_Ward](https://www.ruby-forum.com/u/Matthew_Ward)\
**Post date:** [December 20, 2011, 10:45am UTC](https://www.ruby-forum.com/t/best-way-to-get-the-request-uri-without-the-starting-slash/215003/4 "2011-12-20T10:45:38Z")

</div>

On Tue, Dec 20, 2011 at 04:36:49AM -0500, WheresWardy wrote:

> }  
> Yes, numeric references are not supported in map.

You can also change default to empty string:

map $request\_uri $request\_key {  
default “”;  
~^/(?P.+)$ $key;  
}

–  
Igor S.
