2

我想对我网站上的图像上传器进行 .htaccess 重写,但是对于我尝试过的所有教程,我无法让它工作。

目前,我将图像上传到文件夹的代码: http://www.example.com/server/php/files/jkgh4f54.jpg

我想重写它,所以当您访问此 URL 时:http://www.example.com/jkgh4f54.jpg它显示来自上述位置的图像。

有什么想法吗?正如我所提到的,到处都有很多关于类似内容的教程,但到目前为止,我还无法让它们适应我的工作。

当前的.htaccess:

RewriteEngine On
RewriteCond %(REQUEST_FILENAME} !-f [OR]
RewriteCond %(REQUEST_FILENAME} !-d
RewriteRule ^[a-zA-Z0-9]+\.(jpe?g|png|gif|bmp)$ /server/php/files/$0 [L]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?code=$1 //For URL Shortener
ErrorDocument 404 http://localhost/site.com/404.php
4

2 回答 2

1

使用此代码,当用户访问 时http://example.com/h3h2js.jpg,他们将看到来自 的图像http://example.com/server/php/files/h3h2js.jpg,但浏览器中的 URL 将保持不变:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[a-zA-Z0-9]+\.(jpe?g|png|gif|bmp)$ /server/php/files/$0 [L]

我已经在 Windows 上全新安装 XAMPP 时彻底测试了这段代码。图像名称只能包含任意大小写的字母数字字符(az、AZ、0-9),并且必须以 jpg、jpeg、png、gif 或 bmp 文件扩展名结尾。如果请求的图像文件已经存在于站点根目录中,则不会进行重写

于 2013-01-02T22:34:45.693 回答
0

尝试:

RewriteEngine on
RewriteRule ^(.*)\.jpg$ http://site.com/server/php/files/$1\.jpg [R=301,L]
于 2013-01-02T22:31:30.567 回答