Question

I'm using a PHP script that dynamically generates transparent PNGs for use as CSS backgrounds from a query string that takes RGBa and HSLa values. The original script can be found here, I've only added HSLa support.

Because background URLs with PHP query strings aren't very pretty, and because it seems to break the IE 6 transparent PNG hack, I thought of using mod_rewrite to allow the script to be called when a .png with this syntax is called :

/assets/colors/h[0-360 value]_s[0-100 value]_l[0-100 value]_a[0-100 value].png

which would be rewritten to :

/assets/colors.php?h=[0-360 value]&s=[0-100 value]&l=[0-100 value]&a=[0-100 value]

Here's the issues I'm encountering :

  • passing multiple variables with mod_rewrite
  • using an underscore as a delimiter

I know this could be done by passing a single variable and then exploding it in the PHP script, however I would prefer it to be done by Apache.

Thanks in advance and if anyone wants my HSLa enabled version of the script just ask. Anyway I recommend you check it out on it's author's website.

Was it helpful?

Solution

mod_rewrite will match things in braces (), and then you can refer to these as $1, $2, etc in the order they were matched. So you can use this to extract multiple variables by just placing them inside braces.

So something like this will work for you,

RewriteRule ^assets/colors/h([0-9]{1,3})_s([0-9]{1,3})_l([0-9]{1,3})_a([0-9]{1,3}).png assets/colors.php?h=$1&s=$2&l=$3&a=$4 

([0-9]{1,3}) will match either 1, 2 or 3 of the numbers 0-9 in a row. So "3", "10" and "100", etc. But it would also match "999", so you'll need to make sure the colors.php file checks the values are within expected ranges, etc.

OTHER TIPS

Try this:

RewriteEngine on
RewriteRule /assets/colors/h([0-9]+)_s([0-9]+)_l([0-9]+)_a([0-9]+).png /assets/col.php?h=$1&s=$2&l=$3&a=$4 [QSA,L]

I had to changeup my script name to make this work. Because I have Options MultiViews, /assets/colors/ is always handled by colors.php regardless of the mod_rewrite configuration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top