# How to fix the rewrite rule?

**URL:** <https://www.ruby-forum.com/t/how-to-fix-the-rewrite-rule/221556>\
**Category:** NGINX\
**Created:** [July 19, 2012, 10:33am UTC](https://www.ruby-forum.com/t/how-to-fix-the-rewrite-rule/221556 "2012-07-19T10:33:28Z")\
**Posts on this page:** 3\
**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:** [July 19, 2012, 10:33am UTC](https://www.ruby-forum.com/t/how-to-fix-the-rewrite-rule/221556/1 "2012-07-19T10:33:28Z")

</div>

this works fine  
rewrite “^/([a-f0-9]{2})([a-f0-9]{18}).(jpg|png|gif)$”  
/test.php?a=$1&b=$2&c=$3 last;

open /a669164f02f6fdc18f.jpg

test.php would output  
Array ( [a] =\> ad [b] =\> a669164f02f6fdc18f [c] =\> jpg )

but this DO NOT WORK  
rewrite “^/([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{14}).(jpg|png|gif)$”  
/test.php?a=$1&b=$2&c=$3 last;  
it return 404 error

how can i fix this?

Posted at Nginx Forum:

> **[how to fix the rewrite rule?](https://forum.nginx.org/read.php?2,228725,228725#msg-228725)**
>
> this works fine rewrite "^/(\[a-f0-9\]{2})(\[a-f0-9\]{18})\\.(jpg|png|gif)$" /test.php?a=$1&b=$2&c=$3 last; open /a669164f02f6fdc18f.jpg test.php would output Array ( \[a\] =\> ad \[b\] =\> a669164f02f6fdc18f \[c\] =\> jpg ) but

---

<div class="post-metadata">

**Author:** ![21andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/aca169/32.png) [@21andy](https://www.ruby-forum.com/u/21andy)\
**Post date:** [July 19, 2012, 10:49am UTC](https://www.ruby-forum.com/t/how-to-fix-the-rewrite-rule/221556/2 "2012-07-19T10:49:16Z")

</div>

On Thu, Jul 19, 2012 at 04:32:47AM -0400, 21andy wrote:

Hi there,

> this works fine  
> rewrite “^/([a-f0-9]{2})([a-f0-9]{18}).(jpg|png|gif)$”  
> /test.php?a=$1&b=$2&c=$3 last;
> 
> open /a669164f02f6fdc18f.jpg
> 
> test.php would output  
> Array ( [a] =\> ad [b] =\> a669164f02f6fdc18f [c] =\> jpg )

Really?

Your url has 18 hex characters. Your rewrite rule will only match if  
there are 20. And “[a] =\> ad” doesn’t appear anywhere in your url.

> but this DO NOT WORK  
> rewrite “^/([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{14}).(jpg|png|gif)$”  
> /test.php?a=$1&b=$2&c=$3 last;  
> it return 404 error

Here your rewrite rule will only match if there are 18 hex characters.

I expect

[a] =\> a6 [b] =\> 69 [c] =\> 164f02f6fdc18f

> how can i fix this?

Be consistent.

How long is the url you want to test? Make sure you match exactly that  
many characters in your rewrite rule.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![21andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/2/aca169/32.png) [@21andy](https://www.ruby-forum.com/u/21andy)\
**Post date:** [July 19, 2012, 11:13am UTC](https://www.ruby-forum.com/t/how-to-fix-the-rewrite-rule/221556/3 "2012-07-19T11:13:48Z")

</div>

Hello andi,

your solution looks a bit like my question from last  
night… i use md5 hash to build my filenames, what  
kind of hash do you use? 20 chars look intresing 🙂  
are these unique hashes?

Elena

-------- Original-Nachricht --------

> Datum: Thu, 19 Jul 2012 04:32:47 -0400 (EDT)  
> Von: “21andy” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Betreff: how to fix the rewrite rule?
