Rewrite save original url

When rewriting a url with the rewrite directive, is it possible to save
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
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?


I think you could do something like this:

# 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:


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?

Maxim D.

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

proxy_set_header X-Rewrite-URL $request_uri;

And it worked!

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

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

set $cleanuri $uri;

which in turn i pass to the proxy via headers

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:

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: