# Rewrite save original url

**URL:** <https://www.ruby-forum.com/t/rewrite-save-original-url/195335>\
**Category:** NGINX\
**Created:** [September 21, 2010, 9:22pm UTC](https://www.ruby-forum.com/t/rewrite-save-original-url/195335 "2010-09-21T21:22:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruhtra01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@ruhtra01](https://www.ruby-forum.com/u/ruhtra01)\
**Post date:** [September 21, 2010, 9:22pm UTC](https://www.ruby-forum.com/t/rewrite-save-original-url/195335/1 "2010-09-21T21:22:40Z")

</div>

When rewriting a url with the rewrite directive, is it possible to save  
the  
original url in a request header for the downstream handler?

Similar to the X-Rewrite-URL header built in to ISAPI rewrite.  
Or in Apache, I can do it with something like:

#== Capture original uri and query string into environment variable so  
it  
can be put in a header  
#== Match non-empty query string  
RewriteCond %{QUERY\_STRING} (^.+$)  
RewriteRule (.\*) $1 [E=x-rewrite-url:$1?%1]

If this is possible in nginx, how would I do it?

Thanks

---

<div class="post-metadata">

**Author:** ![ruhtra01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@ruhtra01](https://www.ruby-forum.com/u/ruhtra01)\
**Post date:** [September 21, 2010, 10:14pm UTC](https://www.ruby-forum.com/t/rewrite-save-original-url/195335/2 "2010-09-21T22:14:12Z")

</div>

I think you could do something like this:

```auto
# save old uri + arguments:
set $original_uri $uri$is_args$args;
# perform rewrite:
rewrite ^(.*)$ /new_uri/index.php;

# later on, in location defined downstream handler, add:
proxy_set_header X-Rewrite-URL $original_uri;
```

Never try it out, but hope it helps!

Posted at Nginx Forum:

> **[Re: rewrite save original url](https://forum.nginx.org/read.php?2,132694,132707#msg-132707)**
>
> When rewriting a url with the rewrite directive, is it possible to save the original url in a request header for the downstream handler? Similar to the X-Rewrite-URL header built in to ISAPI rewrite. Or in Apache, I can do it with something like: #==...

---

<div class="post-metadata">

**Author:** ![ruhtra01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@ruhtra01](https://www.ruby-forum.com/u/ruhtra01)\
**Post date:** [September 21, 2010, 10:22pm UTC](https://www.ruby-forum.com/t/rewrite-save-original-url/195335/3 "2010-09-21T22:22:24Z")

</div>

Hello!

On Tue, Sep 21, 2010 at 03:21:31PM -0400, Arthur B. wrote:

> RewriteRule (.\*) $1 [E=x-rewrite-url:$1?%1]
> 
> If this is possible in nginx, how would I do it?

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Maxim D.

---

<div class="post-metadata">

**Author:** ![ruhtra01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@ruhtra01](https://www.ruby-forum.com/u/ruhtra01)\
**Post date:** [September 22, 2010, 2:34am UTC](https://www.ruby-forum.com/t/rewrite-save-original-url/195335/4 "2010-09-22T02:34:47Z")

</div>

Thank you Maxim and tqvn2004! I placed the following in my location  
directive:

proxy\_set\_header X-Rewrite-URL $request\_uri;

And it worked!

---

<div class="post-metadata">

**Author:** ![ruhtra01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@ruhtra01](https://www.ruby-forum.com/u/ruhtra01)\
**Post date:** [October 13, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/rewrite-save-original-url/195335/5 "2010-10-13T20:02:32Z")

</div>

I found a way. Never used “set” variables before… need to study nginx  
more 😛

Just before the rewrites i’m saving the original $uri to a variable

```auto
set $cleanuri $uri;
```

which in turn i pass to the proxy via headers

```auto
proxy_set_header X-Rewrite-FullURI $request_uri;
proxy_set_header X-Rewrite-CleanURI $cleanuri;
```

p.s: all my content rewrites are using “last”;

Posted at Nginx Forum:

> **[Re: rewrite save original url](https://forum.nginx.org/read.php?2,132694,140164#msg-140164)**
>
> When rewriting a url with the rewrite directive, is it possible to save the original url in a request header for the downstream handler? Similar to the X-Rewrite-URL header built in to ISAPI rewrite. Or in Apache, I can do it with something like: #==...

---

<div class="post-metadata">

**Author:** ![ruhtra01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@ruhtra01](https://www.ruby-forum.com/u/ruhtra01)\
**Post date:** [October 13, 2010, 7:31pm UTC](https://www.ruby-forum.com/t/rewrite-save-original-url/195335/6 "2010-10-13T19:31:46Z")

</div>

Is it possible to have only the original URL without the arguments?

I know two ways but i rather use a ready made variable from nginx.  
PHP and rewrite with regex

Thanks in advance

Posted at Nginx Forum:

> **[Re: rewrite save original url](https://forum.nginx.org/read.php?2,132694,140151#msg-140151)**
>
> When rewriting a url with the rewrite directive, is it possible to save the original url in a request header for the downstream handler? Similar to the X-Rewrite-URL header built in to ISAPI rewrite. Or in Apache, I can do it with something like: #==...
