[英]Using Mod_Rewrite With Image URL Parameters
I'm using a script to watermark images on the fly through a URL. 我正在使用脚本通过URL动态为图像加水印。
For example: http://example.com/watermark.php?filename=assets/img/temp/temp_share.jpg
would result in temp_share.jpg
having a watermark on the new image. 例如: http://example.com/watermark.php?filename=assets/img/temp/temp_share.jpg
: http://example.com/watermark.php?filename=assets/img/temp/temp_share.jpg
temp_share.jpg
将导致temp_share.jpg
在新图像上带有水印。
I'm trying to use mod_rewrite
to clean up the URL to something like http://example.com/filename/assets/img/temp/temp_share.jpg
我正在尝试使用mod_rewrite
将URL清理为类似http://example.com/filename/assets/img/temp/temp_share.jpg
I used the generator at this site and it gave me RewriteRule ^filename/([^/]*)$ /watermark.php?filename=$1 [L]
. 我在此站点上使用了生成器,它给了我RewriteRule ^filename/([^/]*)$ /watermark.php?filename=$1 [L]
。
But when I try to go to http://example.com/filename/assets/img/temp/temp_share.jpg
I get an error of Not Found The requested URL /filename/assets/img/temp/temp_share.jpg was not found on this server.
但是,当我尝试访问http://example.com/filename/assets/img/temp/temp_share.jpg
,出现错误“ Not Found The requested URL /filename/assets/img/temp/temp_share.jpg was not found on this server.
I'm not sure what I've done wrong. 我不确定自己做错了什么。
Full contents of my .htaccess 我的.htaccess的完整内容
ErrorDocument 400 /err.php?err=400
ErrorDocument 401 /err.php?err=401
ErrorDocument 403 /err.php?err=403
ErrorDocument 404 /err.php?err=404
ErrorDocument 500 /err.php?err=500
ErrorDocument 502 /err.php?err=502
ErrorDocument 504 /err.php?err=504
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule assets/styles/style.css assets/styles/style.php [L]
RewriteRule assets/styles/m.style.css assets/styles/m.style.php [L]
RewriteRule ^filename/([^/]*)$ /watermark.php?filename=$1 [L]
<FilesMatch ".(jpg|png|gif|jpeg)$">
ErrorDocument 404 error/stormtrooperBrokenImage.jpg
</FilesMatch>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin http://lucycypher.com
Header set Access-Control-Allow-Credentials true
</IfModule>
Contents of watermark.php
: watermark.php
内容:
$imgpath=$_REQUEST['filename'];
header('content-type: image/jpeg');
$watermarkfile="assets/img/logo_variations/logo_watermark_75.png";
$watermark = imagecreatefrompng($watermarkfile);
list($watermark_width,$watermark_height) = getimagesize($watermarkfile);
$image = imagecreatefromjpeg($imgpath);
$size = getimagesize($imgpath);
$dest_x = ($size[0] - $watermark_width)/2;
$dest_y = ($size[1] - $watermark_height)/2;
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
[^/]
is a character class of all characters except for / [^/]
是除 / 以外的所有字符的字符类。
Since your file names do contain / you need to capture all characters after the "filename/" token. 由于您的文件名确实包含/,因此您需要捕获“ filename /”标记之后的所有字符。 Try changing it to this. 尝试将其更改为此。
RewriteRule ^filename/(.*)$ /watermark.php?filename=$1 [L]
After tripple checking that your rewrite module is enabled and you have done a server restart, if it still doesn't work you should consider enabling verbose rewrite debugging to log files. 在三重检查您的重写模块已启用并且服务器已重新启动之后,如果仍然无法正常工作,则应考虑对日志文件启用详细的重写调试。
You should also consider adding an extra security check on the image path. 您还应该考虑在映像路径上添加额外的安全检查。 It is very easy for somebody to manipulate the filename param with things like ../../../sensitive_uploads/1.jpg
to navigate to somewhere outside of your image directory. 对于某些人来说,使用../../../sensitive_uploads/1.jpg
东西来操纵文件名参数很容易,可以导航到图像目录之外的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.