Regex 通过htaccess重定向两次
我正在尝试动态生成缩略图图像。有一个简化的例子。 用户请求 /fakeimagespath/image_name.400x400.jpg 其中400x400是所需的缩略图大小。 如果此映像存在于/realpath/image_name.400x400.jpg文件夹中,我们将通过.htaccess内部重定向显示它 如果此映像不存在,我希望内部重定向到 /generator.php?name=image\u name&size=400x400 这个php文件将从源文件生成它,并以jpeg mime类型输出 请帮助了解mod_revrite规则Regex 通过htaccess重定向两次,regex,apache,.htaccess,mod-rewrite,Regex,Apache,.htaccess,Mod Rewrite,我正在尝试动态生成缩略图图像。有一个简化的例子。 用户请求 /fakeimagespath/image_name.400x400.jpg 其中400x400是所需的缩略图大小。 如果此映像存在于/realpath/image_name.400x400.jpg文件夹中,我们将通过.htaccess内部重定向显示它 如果此映像不存在,我希望内部重定向到 /generator.php?name=image\u name&size=400x400 这个php文件将从源文件生成它,并以jpeg mime类
#index.php prints $_SERVER for debug purposes.
#cup.jpg, girl.jpg - existing files, 404.jpg - not exists
RewriteEngine on
RewriteCond $1 ^fakeimage
RewriteRule ^(fakeimage\-(cup|girl|404)\.jpg)$ /realpath/$2.jpg
RewriteCond $1 -f
RewriteRule ^(.*)$ /index.php?found=$1 [L]
RewriteCond $1 !-f
RewriteRule ^(.*)$ /index.php?notfound=$1 [L]
更新(示例):
/shop/some-user-friendly-alias.999.400x500.jpg
我将其重写为REAL path/images/999/400x500.jpg
但此图像不存在,我想动态生成缩略图。以下是我使用的脚本。你可以根据自己的需要进行调整 使用以下代码将.htaccess放入
/
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^imgresize/(.+)$ imgresize/imgresize.php
以下代码位于/imgresize/imgresize.php
<?php
// no image requested
if(!isset($_SERVER['REQUEST_URI']) || empty($_SERVER['REQUEST_URI'])) {
header('HTTP/1.0 404 Not Found');
exit;
}
// thumbnail already exists (should never be called as the .htaccess is handling this)
if(file_exists('../'.$_SERVER['REQUEST_URI'])) {
header('Content-type: image');
readfile('../'.$_SERVER['REQUEST_URI']);
exit;
}
// extract new width, new height and filename from request
preg_match_all('%/imgresize/([0-9]+)x([0-9]+)/(.+)$%isU', $_SERVER['REQUEST_URI'], $matches);
$new_width = $matches[1][0];
$new_height = $matches[2][0];
$filename = $matches[3][0];
// file doesn't exist
if(!file_exists('../'.$filename)) {
header('HTTP/1.0 404 Not Found');
exit;
}
// get width, height and file format from the original image
list($ori_width, $ori_height, $type, $attr) = getimagesize('../'.$filename);
// create new image
if($type == IMAGETYPE_JPEG) {
$ori_image = imagecreatefromjpeg('../'.$filename);
}
elseif( $type == IMAGETYPE_GIF ) {
$ori_image = imagecreatefromgif('../'.$filename);
}
elseif( $type == IMAGETYPE_PNG ) {
$ori_image = imagecreatefrompng('../'.$filename);
}
// calculate new image ratio
$src_x = $src_y = 0;
if($ori_height/$new_height > $ori_width/$new_width) {
$old_height = $ori_height;
$ori_height = $ori_width/($new_width/$new_height);
$src_y = $old_height/2 - $ori_height/2;
}
else {
$old_width = $ori_width;
$ori_width = $ori_height/($new_height/$new_width);
$src_x = $old_width/2 - $ori_width/2;
}
// resize original image
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $ori_image, 0, 0, $src_x, $src_y, $new_width, $new_height, $ori_width, $ori_height);
// create path
$new = $new_width.'x'.$new_height.'/'.$filename;
$parts = explode('/', $new);
$path = '';
for($i=0;$i<sizeof($parts)-1;$i++) {
$path .= $parts[$i].'/';
if(!file_exists($parts[$i])) {
mkdir($path);
}
}
// save the created image
imagejpeg($new_image, $new, 90);
// sent the created image to the user
header('Content-Type: image');
imagejpeg($new_image, null, 90);
?>
类似的东西应该适合您:
RewriteEngine On
# images exists in /realpath/
RewriteCond %{DOCUMENT_ROOT}/realpath/$1 -f
RewriteRule ^fakeimages/([^.]+\.[^.]+\.jpg)$ /realpath/$1 [L,NC]
# images doesn't exist in /realpath/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^fakeimages/([^.]+)\.([^.]+)\.jpg$ /generator.php?name=$1&size=$2 [L,NC,QSA]
解决了。很抱歉给您带来不便。
由两个.htaccess文件完成。
第一个-内部根目录:/.htaccess
RewriteEngine on
RewriteCond $1 ^fooimage
RewriteRule ^(fooimage\-(cup|girl|404)\.jpg)$ /realpath/$2.jpg [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^generate\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ generate.php?notfound=$1 [L]
realpath内部第二个:/realpath/.htaccess
RewriteEngine on
RewriteCond $1 ^fooimage
RewriteRule ^(fooimage\-(cup|girl|404)\.jpg)$ /realpath/$2.jpg [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^generate\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ generate.php?notfound=$1 [L]
所以,若并没有找到真实的图像,脚本将生成它 谢谢你,Reeno,我确信这段代码是有效的,但是我不能使用这个解决方法,因为图像应该有别名,而不是真实的文件名。示例:/shop/some-user-friendly-alias.999.400x500.jpg,因此我将其重写为REAL path/images/999/400x500.jpg,但此图像不存在,我希望生成它。您可以根据需要调整正则表达式。Yupp!但我无法检查此重写条件%{REQUEST_FILENAME}-f、 因为这个文件不存在,我需要两个级别的重写^我们需要更深入。首先将generator.php重写为real path(如果该路径不存在)