Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule

if=then=else redirect

         

Trusty

8:20 pm on Jan 6, 2004 (gmt 0)



Hello!

I don't know how to write rule if=then=else file exist then redirect.

Let say I have pics_001.jpg and thumbnail tb_pics_001.jpg, so I want site normally show my thumb (tb_pics_001.jpg) in html but if u click on thumb for bigger pics, apache redirect to different URL.

This script redirects all pics, even tb_pics_001.jpg, so I need to add some variable. Any idea from mod_rewrite guru here.

RewriteEngine ON
RewriteCond %{HTTP_REFERER} ^http://www.mysite.com/gallery.html.*$ [NC]
RewriteRule ^.*_001.jpg$ http://www.mysite.com/redirect.html [R,L]

[edited by: jdMorgan at 2:29 am (utc) on Jan. 8, 2004]
[edit reason] de-linked [/edit]

jdMorgan

4:33 am on Jan 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'd need to add a RewriteCond to stop redirection of tb_pics:
 RewriteCond %{REQUEST_URI} !tb-pics 

See the warnings it this thread: [webmasterworld.com...]

Jim

Trusty

10:39 am on Jan 7, 2004 (gmt 0)



Thanks jdMorgan, but I already solve.

below is working code:

RewriteEngine ON
RewriteCond %{HTTP_REFERER} ^http://www.mysite.com.*$ [NC]
RewriteRule ^t_.*\.jpg - [L]
ReWriteRule ^.*_001.jpg$ http://www.mysite.com/redirect.html [R,L]

- means do nothing, [L] last.

[edited by: jdMorgan at 2:29 am (utc) on Jan. 8, 2004]

jdMorgan

4:50 pm on Jan 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Trusty,

Yes, but the problem is that you will not be able to add any more RewriteRules after that, because your first rule will stop rewrite processing for all files except t_.*\.jpg files.

A more general, solution is:


RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://www\.example\.com [NC]
RewriteCond %{REQUEST_URI}!^/t_.*\.jpg$
RewriteRule _001\.jpg$ http://www.mysite.com/redirect.html [R,L]

This code does the same thing, but allows you to add more RewriteRules later.

Note: "^.*" and ".*$" are completely redundant. Leave them off. Ref: Regular Expressions tutorial [etext.lib.virginia.edu]

Jim