Rewrite or Redirect Image files on Website to Homepage

Written by Allen White on. Posted in Web

On another site I administrate, the website shows images then the user moves on. I had an issue where if an image on the site was copied to facebook then when a facebook user clicked on the link then they where taken directly to the image and not the page the image was on. Basically it was just the image loaded in the web browser. This is not good for users as they had no other option than to leave the page and not good for my traffic stats!

To fix this I added the following rule to my .htaccess file. I wanted any user that is not coming from my site sent back to the homepage. An issue I had to overcome was allowing my site to still load images from itself.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpe?g|gif|png)$ http://www.mydomain.co.uk [NC,L]

Now ill explain what above code does.

RewriteCond %{HTTP_REFERER}

Allows my site to load from itself so it does not redirect any requests from itself.

RewriteRule \.(jpe?g|gif|png)$ http://www.mydomain.co.uk [NC,L]

Tells any request from the listed image files to be redirected to the homepage of the website. Indeed this code could be adapted to redirect certain images to certain pages if required.

 
Allen White
at
Allen is a Consultant for ITPS in the North East of England and holds the following accreditations. MCSA, MCSE, MCTS, MCITP, CCA, CCSP, VCP 4,5 and HP ASE, AIS - Network Infrastructure.

ITPS provides strategic IT consultancy, implementation, data centre provision and unified communications, as well as support services and workspace and disaster recovery. If you require a consultation then please contact me via the contacts section or direct on 07931222991, add me on linkedin. https://uk.linkedin.com/in/allenwhiteconsultant0001

Tags:

Leave a comment

Categories

(c) Techieshelp.com. Please be aware, all information is provided freely, any information used is done so at your risk and Techieshelp will not be held responsible for any issue that may occur.
!-- BuySellAds On-Site Shopping Cart -->