mod_rewrite,无法重写现有文件 [英] mod_rewrite, can't rewrite existing file
问题描述
我正在尝试使用.htaccess中的mod_rewrite将目录中的所有图像传递到watermark.php.
I'm trying to pass all images in a directory to watermark.php using mod_rewrite in .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.jpg$ /watermark.php?path=%{REQUEST_FILENAME} [L]
它在本地计算机上运行良好,但是在我的在线生产服务器(共享主机)上,所有图像文件都无需重写就可以提供.
It works fine on local machine, but on my online production server (shared hosting) all image files are served without rewriting.
mod_rewrite在线启用,但是如果文件存在,它将忽略该规则.
mod_rewrite is enabled online, but it ignores the rule if file exists.
有什么问题吗?
更新
这是我的完整设置:在主域的document_root的子文件夹中有一个带有子域的域.
Here's my full setup: there's a domain with a subdomain in a subfolder of main domain's document_root.
public_html (example.com DOCUMENT_ROOT)/
img (img.example.com DOCUMENT_ROOT)/
.htaccess在public_html文件夹中:
.htaccess in public_html folder:
<FilesMatch "\.(inc\.php|log)$">
Deny from all
</FilesMatch>
Options -Indexes +FollowSymLinks
php_value short_open_tag 0
php_value auto_prepend_file /home/username/public_html/bootstrap.inc.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^img\. [NC]
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\. [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ shop/$1 [L]
.htaccess: .htaccess in 问题是 The problem is that existing images in 您可以使用以下方法: 第一个 The first 第二个图像会将带有扩展名jpg,jpeg,png和gif的字母,数字,破折号,下划线和点匹配的所有图像重定向到watermark.php文件.例如: The second one will redirect any images that match letters, numbers, dash, underscore and dot with the extension: jpg, jpeg, png and gif to your watermark.php file. eg: http://www.domain.com/logo.jpg =>路径=/logo.jpg http://www.domain.com/logo.jpg => path = /logo.jpg 更新: 因为该规则位于其他文件夹中,并且IMG的根目录为 Since the rule is in a different folder and the root directory of IMG is 这篇关于mod_rewrite,无法重写现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!img
folder:<FilesMatch "\.(inc|log)$">
Deny from all
</FilesMatch>
Options -Indexes +FollowSymLinks
php_value short_open_tag 0
php_value auto_prepend_file none
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.jpg$ /watermark.php?path=%{REQUEST_FILENAME} [L]
img
文件夹中的现有图像未重写. Apache照原样提供服务.但是,例如,如果我请求不存在的文件,则该规则有效,并且echo $_GET['path']
将完整文件路径打印到不存在的文件. img
folder are not rewrited. Apache just serve them as is. But if, for example, I request non-existing file the rule works and echo $_GET['path']
prints the full filepath to non-existing file. 推荐答案
RewriteEngine On
RewriteRule ^(media|image|images)(/?)(.*)$ - [F]
RewriteRule ^([a-z0-9\-_\.]+)\.(jpg|jpeg|png|gif)$ /watermark.php?path=%{REQUEST_URI} [L]
RewriteRule
设置为确保人们不会进入该文件夹-它拒绝了403权限(这是可选的)RewriteRule
is set to make sure people don't go to that folder - it gives a 403 permission denied (this is optional)/home/username/public_html/img/
,所以watermark.php文件不存在.将所需的文件(watermark.php及其库)复制到IMG文件夹或创建symblink./home/username/public_html/img/
the watermark.php file does not exists. Either copy the file you need (watermark.php and its libraries) to the IMG folder or create symblink.cd /home/username/public_html/img/
ln -s ../watermark.php (and other library files as well)