【发布时间】:2017-01-28 23:23:33
【问题描述】:
在我的服务器上,我使用 TimThumb 作为调整图像大小的插件,它非常有用,除非我尝试在我的电子邮件内容中使用它。
Google Gmail 将此https://example.com/thumb.php?sr+c= 输出为src 属性(注意加号)。
我读到here 是因为查询。
那么如何使用.htaccess 重写我的网址并使用/src/ 删除/thumb.php?src=?
这是图片链接的样子:
https://example.com/thumb.php?src=example.jpg
这就是我需要的
https://example.com/src/example.jpg
这是我目前的.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome&filter=$1 [NC,QSA,L]
RewriteRule ^explore/([^/]+)/?$ index.php?a=explore&filter=$1 [NC,QSA,L]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC,QSA,L]
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]
index.php 和 thumb.php 都在根文件夹中
更新
我尝试将此行添加到我的.htaccess 并访问https://example.com/src/example.jpg,但它再次不起作用,在这种情况下它重定向到“欢迎”页面。
RewriteRule ^src/([^/]*)$ /thumb.php?src=$1 [L]
第二次更新
我也试过这个:
RewriteRule ^src/([^/]+)/?$ thumb.php?src=$1 [NC,QSA,L]
它再次不起作用 https://example.com/src/example.jpg 重定向到我的“欢迎”页面。
这就是我的 .htaccess 现在的样子
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome&filter=$1 [NC,QSA,L]
RewriteRule ^explore/([^/]+)/?$ index.php?a=explore&filter=$1 [NC,QSA,L]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC,QSA,L]
RewriteRule ^src/([^/]+)/?$ thumb.php?src=$1 [NC,QSA,L]
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]
第三次更新
在thumb.php 内部,这是我构建 src 查询的方式
$_GET['src'] = 'https://s3-eu-west-1.amazonaws.com/bucket1'.'/'.$_GET['src'];
我不知道这里出了什么问题。
第四次更新
根据 Vladimir Cvetic 的回答,我尝试了这个:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^src/(.*)$ /thumb.php?src=$1 [L,QSA]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome&filter=$1 [NC,QSA,L]
RewriteRule ^explore/([^/]+)/?$ index.php?a=explore&filter=$1 [NC,QSA,L]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC,QSA,L]
RewriteRule ^src/([^/]+)/?$ thumb.php?src=$1 [NC,QSA,L]
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]
再一次它不起作用,这是错误:
`The 't' parameter is not set.`
但很奇怪,因为我将't'参数条件设置为空,如下所示:
if(!empty($_GET['t'])) {
if($_GET['t'] == 'a') {
$type = 'uploads/avatars';
$width_list = array(25, 35, 50, 70, 100, 112, 150, 200, 300);
$height_list = array(25, 35, 50, 70, 100, 112, 150, 200, 300);
} elseif($_GET['t'] == 'b') {
$type = 'uploads/backgrounds';
...
} else {
exit('Invalid parameter value');
}
} else {
$_GET['t'] == 'a';
$type = 'uploads/avatars';
$width_list = array(25, 35, 50, 70, 100, 112, 150, 200, 300);
$height_list = array(25, 35, 50, 70, 100, 112, 150, 200, 300);
}
确实,如果我访问https://example.com/thumb.php?src=example.jpg,我可以正确看到头像图像
【问题讨论】:
-
不是直接回答,而是:停止使用 Timthumb。这是numerous security issues 和has been unmaintained since 2014 的主题。此时继续使用它是非常危险的。
-
你把新规则放在哪里了?如果您只是在最后附加它,那么在它之前的规则很可能已经将请求重写到 index.php。尝试至少将其放在最后一条现有规则之前。
-
对 .htaccess 的更改不需要重新启动网络服务器,每次请求都会读取 .htaccess 文件。 // 您能否编辑问题以显示您尝试过的完整修改规则集?
-
好吧,如果请求被重写为 index.php 而不是 thumb.php (这很可能是这里的情况),那只会成为一个问题。我会启用重写日志记录以检查发生了什么;它可能需要在顶部添加一个 RewriteCond 来检查内部重写请求是否已经指向 thumb.php。
标签: php .htaccess mod-rewrite timthumb