开发者

mod_rewrite, can't rewrite existing file

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 is enabled online, but it ignores the rule if file exists.

What could be wrong?

UPDATE

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 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 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]    

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.


Here's what you can use:

RewriteEngine On
RewriteRule ^(media|image|images)(/?)(.*)$ - [F]
RewriteRule ^([a-z0-9\-_\.]+)\.(jpg|jpeg|png|gif)$ /watermark.php?path=%{REQUEST_URI} [L]

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

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 => path = /logo.jpg

UPDATE:

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)


htaccess change :

Options -Indexes +FollowSymLinks

by

Options -Indexes +FollowSymLinks -MultiViews

This behaviour steel exists on Apache 2.4..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜