Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite (for an image)

         

whoo

1:04 am on Oct 5, 2007 (gmt 0)

10+ Year Member



I know that I can do this:

RewriteRule ^(.*)/(.*)/(.*)/(.*)$ thing.php?$1=$2&$3=$4

to rewrite this:

< [yourdomain.com...] >

to this:

< [yourdomain.com...] >

how can I edit that rule so that I am rewriting from this:

< [yourdomain.com...] >

--

The script I am attempting to do this with generates an image on the fly. I would like the .png/.jpg/.gif extension to be appended.

Thanks in advance :)

whoo

1:09 am on Oct 5, 2007 (gmt 0)

10+ Year Member



In experimenting with this myself,

RewriteRule ^(.*)/(.*)/(.*)/(.*\.png)$ thing.php?$1=$2&$3=$4

doesnt break the script entirely.. In fact, its displaying the image when you call it up, except that the last query (4) doesnt seem to be beiung respected -- that might be the fault of the php script though.

Assuming the example above is how i ought to be doing this?

whoo

1:54 am on Oct 5, 2007 (gmt 0)

10+ Year Member



doing some more testing ..

the first variable is successfully being passed to the PHP script responsible for the output (it's using simple $_GET's)

however, the last variable isnt being passed .. the PHP script is set up to output a default setting if that variable isnt set, and additionally, thats what it's echoing back, so Ive verified this 2x)

Im at a loss.

whoo

1:58 am on Oct 5, 2007 (gmt 0)

10+ Year Member



ok, more troubleshooting..:)

THIS:

RewriteRule ^(.*)/(.*)/(.*)/(.*\.png)$ thing.php?$1=$2&$3=$4 [L]

actually sends a GET of $somecolor.png

and now I see why.

Im guessing I need to move the .png out of the atom?

whoo

2:01 am on Oct 5, 2007 (gmt 0)

10+ Year Member



tada! that was the solution :)

RewriteRule ^(.*)/(.*)/(.*)/(.*).png$ thing.php?$1=$2&$3=$4 [L]

For anyone else that stumbles across this post, sometimes you just have to do a little thinking and use some fundamental troubleshooting techniques to figure out the solution.

jdMorgan

3:23 am on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't use "(.*)/(.*)"-type patterns if you can avoid it, since they invoke many attempts to find a match. Instead, be explicit.

This will be processed many, many times faster:


^([^/]+)/([^/]+)/([^/]+)/([^.]+)\.png$

Here we use negative-match subpatterns to tell the regex parser "stop matching for this subpattern when the next character is a slash (a period in the last subpattern). This allows a fast, single, left-to-right evaluation of the requested URL-path against the pattern.

If you use "(.*)/(.*)"-type patterns, the parser will initially attempt to match all of the requested URL-path into the first "(.*)" subpattern because ".*" means "Match any characters, and as many as possible."

This will fail, so the parser will 'move' one character out of the first subpattern and try again. This try-and-fail will repeat until the URL-path is correctly apportioned to the two "(.*)" subpatterns and results in a match, or until all possible combinations are tried and fail.

Now imagine what happens when you have three "(.*)" patterns in a row -- The number of trial matches goes way up. With four or more, it can become a huge number -- tens of thousands.

Also, be aware that the pattern "^(.*)/(.*)/(.*)/(.*\.png)$" will match any of the following requested URLs, which is probably not what you want:
a/b/c/d.png (expected URL format)
a/b/c/d/e.png (one 'directory' longer
a/b/c/d/e/f.png (two 'directories' longer)
etc.

In other words, any URL of the form a/b/c/d.png or longer. You will find that the 'extra' path-parts end up in $1. For example, the second example URL-path "a/b/c/d/e.png" will result in a rewrite to "thing.php?a/b=c&d=e"

".*" subpatterns are 'easy' but can dangerous. If used multiple times in one pattern, they can be very slow to process and can produce unexpected results. Avoid them whenever possible.

Jim