mod_rewrite,无法重写现有文件 [英] mod_rewrite, can't rewrite existing file

查看:101
本文介绍了mod_rewrite,无法重写现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.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 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']将完整文件路径打印到不存在的文件.

The problem is that existing images in 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权限(这是可选的)

The first RewriteRule is set to make sure people don't go to that folder - it gives a 403 permission denied (this is optional)

第二个图像会将带有扩展名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的根目录为/home/username/public_html/img/,所以watermark.php文件不存在.将所需的文件(watermark.php及其库)复制到IMG文件夹或创建symblink.

Since the rule is in a different folder and the root directory of IMG is /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)

这篇关于mod_rewrite,无法重写现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆