Apache - rewrite images to php file with .htaccess Apache - rewrite images to php file with .htaccess apache apache

Apache - rewrite images to php file with .htaccess


Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews# Turn mod_rewrite onRewriteEngine OnRewriteBase /RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]

Make sure:

  • .htaccess is enabled
  • mod_rewrite is enabled
  • Your URL is http://example.com/folder/img/test.jpg


It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:

RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1


Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.

RewriteEngine onRewriteBase /RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]

If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.

Hope this helps.

Bye