具有多个变量的 Mod_rewrite
我正在使用一个 PHP 脚本,该脚本可以从采用 RGBa 和 HSLa 值的查询字符串动态生成透明 PNG,用作 CSS 背景。原始脚本可以在这里找到,我已经仅添加了 HSLa 支持。
.png 时调用脚本:
/assets/colors/h[0-360 value]_s[0-100 value]_l[0-100 value]_a[0-100 value].png
因为带有 PHP 查询字符串的后台 URL 不是很漂亮,而且因为它似乎破坏了 IE 6 透明 PNG 黑客,所以我想到使用 mod_rewrite 来允许在调用具有此语法的 被重写为:
/assets/colors.php?h=[0-360 value]&s=[0-100 value]&l=[0-100 value]&a=[0-100 value]
这是我遇到的问题:
- mod_rewrite 传递多个变量
- 使用下划线作为分隔符使用
我知道这可以通过传递单个变量然后在 PHP 脚本中分解它来完成,但我更希望它是由阿帕奇完成。
提前致谢,如果有人想要我的启用 HSLa 的脚本版本,请询问。无论如何,我建议您在其作者的网站上查看。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mod_rewrite 将匹配大括号 () 中的内容,然后您可以按照它们匹配的顺序将它们称为 $1、$2 等。因此,您可以使用它来提取多个变量,只需将它们放在大括号内即可。
因此,类似这样的内容将适合您,
([0-9]{1,3})
将匹配连续数字 0-9 中的 1、2 或 3 个。所以“3”、“10”和“100”等。但它也会匹配“999”,因此您需要确保colors.php文件检查值是否在预期范围内,等等。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,
([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.试试这个:
我必须更改我的脚本名称才能使其工作。因为我有
Options MultiViews
,所以 /assets/colors/ 始终由 color.php 处理,无论 mod_rewrite 配置如何。Try this:
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.