\就我个人而言,我会将这个任务委托给一个脚本(可能使用X-Sendfile),而不是一个重写混乱的脚本。
接下来,您可能需要调整文档根目录,使“profile”部分更通用:
RewriteEngine on
RewriteCond %{QUERY_STRING} "(?:^|&)crop&p=50"
RewriteCond %{REQUEST_URI} ^(/img/profile)(\.png)$
RewriteCond /your/document/root%1.c.50%2 -f
RewriteRule ^ %1.c.50%2 [L]
# hand over to crop script if the above doesn't match
RewriteCond %{REQUEST_URI} =/img/profile.png
RewriteRule ^ /cropper.php?path=%{REQUEST_URI}&p=50 [L]
始终包含脚本的解决方案:
(通过/img/profiles/访问。。。为了清楚起见)
-
$1捕获配置文件名(在本例中假定仅为'a-z'
-
$2捕获所需宽度
-
$3文件扩展名
这确实简化了重写:
RewriteEngine on
RewriteRule ^/img/profiles/([a-z]+)\.([1-9][0-9]+)\.(jpe?g|gif|png)$ /cropper.php?profile=$1.$3&p=$2 [L]
脚本将检查文件是否存在,要么传递它,要么首先生成它。
从请求的URL获取图像名称的解决方案:
(正如你在评论中提到的)
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteCond /your/document/root%{REQUEST_URI} -f
RewriteRule ^ %{REQUEST_URI} [L]
# hand over to crop script if the above doesn't match
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteRule ^ /cropper.php?path=$1.$3&p=$2 [L]
如果图像直接存储在文档根目录中,则可以重写为以下内容:
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^ /cropper.php?path=$1.$3&p=$2 [L]