Issue with mod_rewrite character limits

Discuss practical ways rearrange URLs using mod_rewrite.

Issue with mod_rewrite character limits

Postby ededdede » Wed Oct 28, 2009 2:48 pm

Hi. We have an issue with a php image generator script, and a cached directory monitored by htaccess . The issue is when a url being fed to htaccess is too long, it results in a Access Forbidded 403 error.

Our rewrite rule in htaccess is:

Code: Select all
RewriteRule ^dimage/(.*).png /_site/image/cached_dynamictext/$1.png [nc,l]

# check if requested file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^_site/image/cached_dynamictext/(.*).png /_site/include/lib/image/dimage.php?array=$1 [nc]   


The theory is that when htaccess comes across a url like
Code: Select all
/dimage/<long string>.php
it will check if an image sits in the /cached_dynamictext/ directory. If it does then we use it. If not we refer to the dimage.php script which displays an image and drops it in the /cached_dynamictext/ directory when it is finished.

The issue is that when a sufficiently long url is passed to the /dimage/ url... We get a 403 error and a log entry:
Code: Select all
The parameter is incorrect.  : Cannot map GET /dimage/<long url>.png HTTP/1.1 to file


HOWEVER the error will disappear if we take away the /cached_text/ check in our htaccess. So,

Code: Select all
RewriteRule ^dimage/(.*).png  /_site/include/lib/image/dimage.php?array=$1 [nc]


Works. Also referring directly to the dimage script will also work. So:

Code: Select all
/_site/include/lib/image/dimage.php?array=<long string>


Works.


Does anyone know if it is possible to get this working? Or any suggestions to try?

Also the long string will fail when it is around 300 characters.
ededdede
 
Posts: 2
Joined: Wed Oct 28, 2009 2:14 pm

Postby richardk » Fri Oct 30, 2009 11:10 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond /full/path/to/_site/image/cached_dynamictext/$1 -f
RewriteRule ^dimage/(.+\.png)$ /_site/image/cached_dynamictext/$1 [NC,L]

RewriteRule ^dimage/(.+)\.png$ /_site/include/lib/image/dimage.php?array=$1 [NC,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby ededdede » Fri Oct 30, 2009 3:55 pm

Hi Thanks for the reply. I think the error has nothing to do with mod rewrite.. but instead is an issue with filenames over 255 characters.

Since we can't use this method and store the entire data array into the filename.. I think what we are going to go with is instead a dual-filename. So we would generate a hash in addition to the image information. So:
Code: Select all
/dimage/<short hash string>/<long image information>/


This way the mod_rewrite will only look up images with the hash... and if that doesnt work it will send the long information to the dimage.php script... which will then generate the file with the name of the hash.

Woot!
ededdede
 
Posts: 2
Joined: Wed Oct 28, 2009 2:14 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron