.htaccess redirection for image link


I would like to know if it is possible to set up a redirect in the .htaccess file so that if another website directly links to an image on my site, instead of the image opening in a browser window on its own, the page that the image is hosted on is displayed.

This page has the same name as the image i.e. if the image the other site is linking to is


www.example.com/subdirABC/gallery/image.jpg **to** www.example.com/subdirABC/ **or** www.example.com/subdirABC/index.php


www.example.com/subdirXYZ/gallery/image.jpg **to** www.example.com/subdirXYZ/ **or** www.example.com/subdirXYZ/index.php


Its work for me

RewriteBase / Options +FollowSymLinks RewriteCond %{HTTP_REFERER} !^https://example.com RewriteRule ^(.*)/gallery/(.*.jpg) /$1/ [L,R=301]


Try something like the following in .htaccess:

RewriteEngine On RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com RewriteRule ^([^/]+/)gallery/[\w-]\.jpg$ /$1 [R,L]

What this says is... for all requests that match the pattern /<subdir>/gallery/<filename>.jpg and are not being linked to from your site (example.com) then redirect to /subdir/.

Additional notes...


Direct links (which includes search engine bots) and user-agents that fail to send the HTTP Referer header will also be redirected to the "page".

</li> <li>

<filename> contains just the characters a-z, A-Z, 0-9, _ and -.

</li> <li>

$1 is a backreference to the first captured group in the RewriteRule <em>pattern</em>, ie. ([^/]+/) - which matches subdirXYZ/ in your example.

</li> <li>

This is a 302 (temporary) redirect.

</li> </ul>


  • Missing Content-Length header when using Nginx + Gzip + Unicorn
  • search text only using Java
  • Django login form in bootstrap popup
  • Error “Call to undefined method stdClass::delete()” while trying delete a row in Laravel
  • find xml element by attribute
  • input type=“file” accept=“image/*” doesn't work in phone gap?
  • Obtain actual browser URL in PHP
  • PHP file_exists() anomaly
  • MVVM: Image Bind Source from FileOpenPicker
  • How to set elevation color?
  • Uncaught TypeError: $(…).select2 is not a function
  • PLSQL: Get number of records updated vs inserted when a merge statement is used
  • How to generate and display a QR Code in ionic 2
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Display java JPanel in a JFrame
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • How to have background script and something similar to a default popup?
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Syntax for setting draggablecursor property in google maps api
  • Paperclip, set path outside of rails root folder
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Optimizing database types to compact database (SQLite)
  • RectangularRangeIndicator format like triangular using dojo
  • Resize panoramic image to fixed size
  • Cross-Platform Protobuf Serialization
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • A cron job substitute?
  • Revoking OAuth Access Token Results in 404 Not Found
  • json Serialization in asp
  • Proper way to use connect-multiparty with express.js?
  • Turn off referential integrity in Derby? is it possible?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Conditional In-Line CSS for IE and Others?