2

I have generated .htaccess rule which redirects requests images from or sources to webpage that consists of that image. However, it is not working, obviously I did something wrong. Any ideas are appreciated!

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/manufacturers/$1/$2/$3.jpg
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteRule (.*) http://www.example.com/$1/$2

Request example: www.example.com/manufacturers/audi/audi-a8/audi-a8-1.jpg

Should redirect to: www.example.com/audi/audi-a8/

Ulugbek
  • 141
  • 11

1 Answers1

2

This line is problem:

RewriteCond %{REQUEST_URI} ^/manufacturers/$1/$2/$3.jpg

Since $1, $2 etc are not available on RHS of RewriteCond. You can use this rule instead:

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example\.com [NC]
RewriteRule ^manufacturers/([^/]+)/[^/]+/([^/]+)/[^/.]+\.jpe?g$ /$1/$2 [L,NC,R=302]

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example\.com [NC]
RewriteRule ^manufacturers/([^/]+/[^/]+)/[^/.]+\.jpe?g$ /$1 [L,NC,R=302]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I have also photos in /manufacturers/$1/$2/$3/$4.jpg which should redirect to http://www.example.com/$1/$3. How can I achieve? Please help – Ulugbek Jan 24 '15 at 07:21
  • I have tried RewriteRule^manufacturers/([^/]+/[^/]+/[^/]+)/.+?\.jpe?g$ /$1[L,NC,R=302] which is redirecting to example.com/$1/$2/$3. What should id example.com/$1/$3 – Ulugbek Jan 24 '15 at 07:36
  • What is exact URL you're redirecting and where should it be redirected to? – anubhava Jan 24 '15 at 07:38
  • Now it redirecting to example.com/$1/$2/. What should is to example.com/$1/$3 – Ulugbek Jan 24 '15 at 07:47
  • If you notice my first rule it is not even capturing 2nd level directory thus 3rd level directory becomes `$2`. As I asked before better to provide **What is exact URL you're redirecting and where should it be redirected to?** – anubhava Jan 24 '15 at 07:53
  • Thank you!!! Everything is working. Honestly, I don't understand how it is working. If possible could you please explain how you achieved this. Thank you one more time – Ulugbek Jan 24 '15 at 07:55
  • Or could you please give some source to understand. – Ulugbek Jan 24 '15 at 07:59
  • This will not stop google indexing my images, right ? – Ulugbek Jan 24 '15 at 08:02
  • No Google or other SEO links will remain fine. For learning mod_rewrite refer http://askapache.com website. And for any doubt create questions on Stackoverflow :) – anubhava Jan 24 '15 at 08:03
  • I have created almost similar question which is not answered. Could you please have a look, perhaps you can answer. The link is [here](http://stackoverflow.com/questions/28146027/watermarking-images-on-the-fly-and-redirect-images-to-the-website-at-the-same-ti) Thank you in advance – Ulugbek Jan 26 '15 at 11:57