# Try\_files rewrite drops other get variables

**URL:** <https://www.ruby-forum.com/t/try-files-rewrite-drops-other-get-variables/223758>\
**Category:** NGINX\
**Created:** [October 16, 2012, 8:44pm UTC](https://www.ruby-forum.com/t/try-files-rewrite-drops-other-get-variables/223758 "2012-10-16T20:44:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 16, 2012, 8:44pm UTC](https://www.ruby-forum.com/t/try-files-rewrite-drops-other-get-variables/223758/1 "2012-10-16T20:44:43Z")

</div>

A couple of wees ago in “Updating some old ‘if’ statements”  
([Updating some old "if" statements](http://forum.nginx.org/read.php?2,231164,231164#msg-231164)) it was  
suggested I use try files instead of the ‘if’ I had.

The old location did two things: It ran an extensionless file as a php  
file and also checked for the existence of a statically cached file.

Though the rewrite is successfully serving the static file and handling  
the extensionless php fine, I didn’t initially notice that other GET  
variables like ?page are getting ignored.

Here’s the old ‘if’ location that passed on all variables just fine:

old:  
location ~ ^/galleries(/._$|$) {  
if (-f /usr/local/nginx/htdocs/pixcache$request\_uri/index.html) {  
expires 2h;  
rewrite ^(._)$ /pixcache$1/index.html last;  
break;  
}  
rewrite ^/galleries(/.\*$|$) /galleries.php?mypath=$1 last;  
}

and the current location with try\_files

location ~ ^/galleries(?P/.\*$|$) {  
expires 2h;  
try\_files /pixcache$request\_uri/index.html  
/galleries.php?mypath=$mypath;  
}

Those familiar with my posts know I suck at regex, but I’m assuming the  
rewrite in the old location successfully took URLS like:

/galleries/129/1/3?page=2 and passed them to php as:  
/galleries.php?mypath=/129/1/3&page=2 while something in the new  
location  
is dropping any additional get variables?

The old location has been working successfully in production for several  
years but I thought I should get rid of the evil if.

The try\_files version, as I said, works except for the dropping of  
additional get variables in the URL.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [October 18, 2012, 3:55am UTC](https://www.ruby-forum.com/t/try-files-rewrite-drops-other-get-variables/223758/2 "2012-10-18T03:55:52Z")

</div>

On 16/10/2012 2:44 PM, Ian M. Evans wrote:

> }
> 
> /galleries/129/1/3?page=2 and passed them to php as:  
> /galleries.php?mypath=/129/1/3&page=2 while something in the new location  
> is dropping any additional get variables?
> 
> The old location has been working successfully in production for several  
> years but I thought I should get rid of the evil if.
> 
> The try\_files version, as I said, works except for the dropping of  
> additional get variables in the URL.

I thought adding $args to the end of the try\_files line would work but  
that appeared to mess it up more. Is there any way to get the try\_files  
version to work like the old version?

Been staring at this but I don’t see how  
rewrite ^/galleries(/.\*$|$) /galleries.php?mypath=$1 last; passes the  
path and additional GET variables, but the try\_files version doesn’t  
pass on the additional variables.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [October 18, 2012, 4:00am UTC](https://www.ruby-forum.com/t/try-files-rewrite-drops-other-get-variables/223758/3 "2012-10-18T04:00:32Z")

</div>

On Thu, Oct 18, 2012 at 8:55 AM, Ian E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I thought adding $args to the end of the try\_files line would work but that  
> appeared to mess it up more. Is there any way to get the try\_files version  
> to work like the old version?
> 
> Been staring at this but I don’t see how  
> rewrite ^/galleries(/.\*$|$) /galleries.php?mypath=$1 last; passes the path  
> and additional GET variables, but the try\_files version doesn’t pass on the  
> additional variables.

From [Alphabetical index of directives](http://nginx.org/r/rewrite:)

“If a replacement string includes the new request arguments, the  
previous request arguments are appended after them. If this is  
undesired, putting a question mark at the end of a replacement string  
avoids having them appended, for example:”

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [October 18, 2012, 4:04am UTC](https://www.ruby-forum.com/t/try-files-rewrite-drops-other-get-variables/223758/4 "2012-10-18T04:04:03Z")

</div>

On Thu, Oct 18, 2012 at 8:59 AM, Edho A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> From [Alphabetical index of directives](http://nginx.org/r/rewrite:)
> 
> “If a replacement string includes the new request arguments, the  
> previous request arguments are appended after them. If this is  
> undesired, putting a question mark at the end of a replacement string  
> avoids having them appended, for example:”

Additionally, I do indeed add $args for my try\_files for wordpress:

try\_files $uri $uri/ /index.php?q=$uri&$args;

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [October 18, 2012, 4:21am UTC](https://www.ruby-forum.com/t/try-files-rewrite-drops-other-get-variables/223758/5 "2012-10-18T04:21:35Z")

</div>

On 17/10/2012 10:03 PM, Edho A. wrote:

> > > additional variables.
> 
> try\_files $uri $uri/ /index.php?q=$uri&$args;

I changed the try\_files to:

try\_files /pixcache$request\_uri/index.html  
/galleries.php?mypath=$mypath&$args;

and it worked. I had mistakenly added the $args without the ‘&’

Will take it for a sail and make sure it keeps working. 🙂
