Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirecting direct image links to the page containing the image

9:01 am on Aug 1, 2013 (gmt 0)

Senior Member

joined:July 29, 2007
votes: 100

Example: Google's image search engine displays images with a 'view full size image' link. Clicking on that link displays the image on its own url only and not the page the image is on. I do not like this use of my images or the backlinks directly to the image url it creates.

I need some help with a solution in htaccess for Google.com.

RewriteCond %{REQUEST_URI} ^/images/my-image.jpg$ [NC]
RewriteCond %{HTTP_REFERER} ^http(s)?://(www\.)?google\.com [NC]
RewriteRule (.*) http://www.example.com/page-the-image-is-on

This *almost* works.
- Images must not be cached by browsers for this to work, check.
- Google image search does not provide a referrer when loading images but they do via the 'show full size image' which is great.
- The code above acts like hotlink protection vs Google but my image does show up in image search, Google uses their cache copy instead.

The above code does exactly what I want it to except for one little problem. When a visitor sees my image in Google image search and they click on the 'view full size image' link they are redirected to the page my image is on... but the image doesn't show up. It shows up if you hit the refresh button in the browser, but I can't figure out why it's not there when redirected with the above.

If caching is enabled then the above does not redirect at all, the image is loaded straight from browser cache and the request is never made. How do I keep cache off but have the image show up immediately without needed to refresh the page? It should be doing that already but it's not. Suggestions?
10:17 am on Aug 1, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

Redirects are cached. When the visitor arrives at your real page and the browser attempts to load the image the browser sees (remembers) the redirect to the HTML page. It follows that, discovers the content is not an image and so does not display it.

Adding additional HTTP headers to prevent the redirect response being cached may help, or may cause yet more problems.
11:27 am on Aug 1, 2013 (gmt 0)

Senior Member

joined:July 29, 2007
votes: 100

I really wish the 'view original image' link pointed at my site to begin with but since it doesn't how about something like...

RewriteCond %{REQUEST_URI} \.(jpe?g|png|gif)$
RewriteCond %{HTTP_REFERER} ^http(s)?://(www\.)?google\.com [NC]
RewriteCond %{HTTP_USER_AGENT} !(Googlebot|Googlebot-Image) [NC]
RewriteRule ^images/(.*).(jpg|png|gif)$ http://www.example.com [L]

With image cache turned off in htaccess this code breaks the 'view original image' link and sends the visitor to your home page. It only works for the view original image link since Google sends no referrer when requesting/displaying the image itself and some minor bot protection *should* keep other problems at bay.

I don't think this improves the user experience but it shouldn't hurt your metrics if visitors backpage since that is the only thing you can do when viewing an image directly anyway. This is nothing more than a hack to break the view original image directly link since that's not something everyone wants happening with their images in the first place, at least not without having a chance at a visitor.

It basically works like frambusting javascript used to on Google images but only for the 'view original' link. Thoughts?
2:44 am on Sept 29, 2013 (gmt 0)

New User

joined:Sept 29, 2013
votes: 0

I have this in a htaccess file inside 8 different image directories. It works beautifully in Google image search, but the redirect is a 302. When I replace [L] with [R=301,L] the image becomes broken when visitors click on "view original image".

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://example.com.*$ [NC]
RewriteRule ^images/(.*)\.(jpe?g|bmp)$ http://example.com/Directory1/pages/$1.htm [L]

I am concerned that Google may have deindexed some images because of this 302. All of the images that were performing well in a particular directory have all now been deindexed and also, one particular image in another directory. This particular directory that was hit, is also the only directory on my site that has numerous backlinks from russian spam sites - so I am not sure if it is related to the redirection or not.

Is there a way to get the images to display properly using R=301?

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members