jaydeep namera
jaydeep namera

Reputation: 1064

Redirect image url to another page using .htaccess

I want to redirect this url

http://192.168.1.101/project/test/wordpress/wp-content/uploads/2013/06/Sunset.jpg

to this with main url as following.

http://192.168.1.101/project/test/wordpress/redirect.php?file=http://192.168.1.101/project/test/wordpress/wp-content/uploads/2013/06/Sunset.jpg

If url contain extension like .jpg, .png, .jpeg, .bmp, .png then move all it to redirect.php with file name like.

http://192.168.1.101/project/test/wordpress/redirect.php?file=imagefile

My current .htaccess code

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project/test/wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /project/test/wordpress/index.php [L]
</IfModule>
# END WordPress

Please Help Me..

Upvotes: 1

Views: 2434

Answers (1)

anubhava
anubhava

Reputation: 785991

Insert this line just after RewriteBase line:

RewriteRule ^wp-content/.+?\.(jpe?g|png|gif|bmp)$ redirect.php?file=http://%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L,NC]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Upvotes: 1

Related Questions