3

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.

sevenWonders
  • 175
  • 1
  • 9

2 Answers2

4

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)
Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
  • I tried it, but still: request for nonexistent file is rewrited to watermark.php just fine, but if existing file is requested - it's served as is, ignoring watermark.php:( – sevenWonders Sep 28 '11 at 11:08
  • @sevenWonders Do you have any other rewrite rules except this one? If yes -- please show ALL of them. – LazyOne Sep 28 '11 at 11:48
  • In addition of other rules (see lazyone comment) what do you see if you echo $_GET['path'] in your watermark.php? – Book Of Zeus Sep 28 '11 at 22:25
  • So example.com document root is the same as img.example.com document root? – Book Of Zeus Oct 03 '11 at 10:36
  • document_root of example.com is `/home/username/public_html/`, and document_root of img.exapmle.com is `/home/username/public_html/img/` – sevenWonders Oct 03 '11 at 10:39
  • The `watermark.php` is already in `/home/username/public_html/img/` – sevenWonders Oct 03 '11 at 11:08
  • Can you show me the code on watermark? Here's mine and it's working fine: http://pastebin.com/LPQavXGR – Book Of Zeus Oct 03 '11 at 11:16
  • Now my watermark.php looks like this:) - `$path = isset($_GET['path']) ? $_GET['path'] : ''; echo $path;` And it just doesn't get there. I suppose something is wrong with subdomains configuration on shared hosting (cause on my local machine everything works just fine). May be I'll try to rewrite the whole thing without subdomains. – sevenWonders Oct 03 '11 at 13:22
  • 1
    I would say YES, double check your subdomain config because I copy and paste this code to mine and it works (I can show it to you if you want) - OR - try to put BOTH subdomain and domain at the same level. EG: www.domain.com = /home/user/domain.com/public_html/ and sub.domain.com => /home/user/domain.com/sub_public_html/ – Book Of Zeus Oct 03 '11 at 23:36
0

htaccess change :

Options -Indexes +FollowSymLinks

by

Options -Indexes +FollowSymLinks -MultiViews

This behaviour steel exists on Apache 2.4..

Bruno
  • 11
  • 3