# Optimize rewrite

**URL:** <https://www.ruby-forum.com/t/optimize-rewrite/227915>\
**Category:** NGINX\
**Created:** [April 3, 2013, 6:22pm UTC](https://www.ruby-forum.com/t/optimize-rewrite/227915 "2013-04-03T18:22:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anto](https://www.ruby-forum.com/u/Anto)\
**Post date:** [April 3, 2013, 6:22pm UTC](https://www.ruby-forum.com/t/optimize-rewrite/227915/1 "2013-04-03T18:22:40Z")

</div>

hello

I have a script that works with apache but I want to migrate to nginx, I  
have this rule, but maybe you can do differently optimized.

## HTACCESS
 RewriteEngine On

RewriteCond %{REQUEST\_FILENAME} !-d  
RewriteCond %{REQUEST\_FILENAME} !-s  
RewriteRule ^(.\*)$ index.php?url=$1 [QSA,NC,L]

RewriteCond %{REQUEST\_FILENAME} -d  
RewriteRule ^(.\*)$ index.php [QSA,NC,L]

RewriteCond %{REQUEST\_FILENAME} -s  
RewriteRule ^(.\*)$ index.php [QSA,NC,L]

## NGINX

location / {  
rewrite ^/(._)/(._)$ /$1/index.php?url=$2;  
}

Thanks !!

Regards

---

<div class="post-metadata">

**Author:** ![Anto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anto](https://www.ruby-forum.com/u/Anto)\
**Post date:** [April 4, 2013, 11:40am UTC](https://www.ruby-forum.com/t/optimize-rewrite/227915/2 "2013-04-04T11:40:23Z")

</div>

> location / {  
> rewrite ^/(._)/(._)$ /$1/index.php?url=$2;  
> }

I would suggest to use try\_files:

location / {  
try\_files $uri $uri/ /index.php?url=$uri&$args;  
}

Personally instead of pasing the uri in the ‘url’ param I like to use  
try\_files $uri $uri/ /index.php?$args; and then in the php code have the  
url  
in $\_SERVER[‘REQUEST\_URI’] (or $\_SERVER[‘DOCUMENT\_URI’]) that way there  
is  
no possibility to accidentaly mess with the GET variables.

rr

---

<div class="post-metadata">

**Author:** ![Anto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anto](https://www.ruby-forum.com/u/Anto)\
**Post date:** [June 20, 2013, 4:38pm UTC](https://www.ruby-forum.com/t/optimize-rewrite/227915/3 "2013-06-20T16:38:07Z")

</div>

Ok, thanks !!! 😉

2013/4/4 Reinis R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
