Forum Moderators: phranque

Message Too Old, No Replies

Second rewrite based on referrer...

         

RedAndy

12:19 am on Jun 16, 2005 (gmt 0)

10+ Year Member



Hi,

I'm just in the process of moving my images from names like 040512.1.jpg to some_really_good_name.jpg and have set up a DB table and php script to handle the redirection. That bit's working fine, however, I have also been using a little .htaccess and php script that checks for the referrer and if it isn't my site writes http://mydomainname onto the bottom of the image.
With the second script in place I get a 500 error, without it all is well, but there's no attribution on the images. I'd really like it to work how it used to :-)

many thanks for your help,

Andy

here's the .htaccess of the second script:
RewriteBase /visible_code/
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^mysite
RewriteRule ^(.*)\.jpg$ att_file.php?file=$1.jpg [L]

[edited by: jdMorgan at 2:41 am (utc) on June 16, 2005]
[edit reason] De-linked. [/edit]

jdMorgan

2:40 am on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Andy,

Welcome to WebmasterWorld!

What's in your server error log when you get the 500 error? It often contains very useful information to aid in debgging problems.

Jim

RedAndy

5:27 am on Jun 16, 2005 (gmt 0)

10+ Year Member



Thanks for your reply & welcome Jim :) , I've read the error message properly ( sorry ), read the log, and turned on LogLevel debug and it says that the redirect limit is being exceeded, together my two rules must be clashing but I'm not sure how. Here is the first ( the second is in my first post ):

RewriteCond %{REQUEST_FILENAME}!-s
RewriteRule ^([a-z0-9_/\.]{3,255}[jpg¦php])$ old_to_new.php?url=$1 [L]

thanks for your help,

Andy

jdMorgan

6:03 am on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a local URL-path associated with that redirection limit error in the log? Full examples of problems are often handy; If we have to pry information out of you, this won't go well...

Here, this code is bad, and might be the cause. I can't be sure:


RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([a-z0-9_/\.]{3,255}[jpg¦php])$ old_to_new.php?url=$1 [L]

Including "jpg¦php" in square brackets means that this rule should match a requested local-URL-path with three to 255 characters matching lowercase letters, underscores, slashes or periods, followed by a single character equal to j, p, g, "¦" p, h, or p. It's more likely that you want to match "jpg" OR "php", so those alternate strings need to be enclosed in parentheses, not square brackets. Parentheses can be nested:

RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([a-z0-9_/\.]{3,255}(jpg¦php))$ old_to_new.php?url=$1 [L]

I would also suggest that you re-examine that pattern, and replace it with something more specific if at all possible. The more specific the pattern, the less often the RewriteCond will be evaluated, and the less often you will be asking your server to search the filesystem to determine if the requested resource is a non-zero-size regular files. These file lookups are quite wasteful of server resources, and so should be avoided.

Then, in order to prevent the looping, exclude the substitution URL from being rewritten:


[b]RewriteCond %{REQUEST_URI} [i][/i]!^old_to_new\.php$[/b]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([a-z0-9_/\.]{3,255}(jpg¦php))$ old_to_new.php?url=$1 [L]

Replace the broken pipe "¦" characters above with solid pipe characters before use. Posting on this board modifies them.

Jim

RedAndy

10:20 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Hi,

sorry for the lack of information - I was trying to be concise and got the balance wrong, I'd only read the error message, log and turned on LogLeve debug after reading your post.

With your help I think the problem is now solved and the pattern is more accurate and a little more efficient. It now reads:

RewriteCond %{REQUEST_URI} ^/[a-z0-9_/-]{3,255}$
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ([a-z0-9_/-]{3,255}) page_handler.php?url=$1

#Redirect old pages into the new format
RewriteCond %{REQUEST_FILENAME}!-s
RewriteRule ^([a-z0-9_/\.]{3,255}(jpg¦php))$ old_to_new.php?url=$1 [L]

#-------------------------------------------------------------------#
# IMAGE ATTRIBUTION#
#-------------------------------------------------------------------#
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?(a)?b(c)?d
RewriteRule ^(.*)\.jpg$ /common_files/functions/attribution.php?file=$1.jpg [L]

-> (www/.)?(a?)b(c?)d
I own www.abcd.com www.bcd.com and www.abd.com but not other variations. I left off the .com because my test server is called abcd and I'm assuming that www.abcd.net etc won't cause me problems. This pattern seems to work, but if it can be improved... :-)

The!-s and!-d inefficiency. The!-s has to go I guess, as the site gets pushed more into the db and is less in the 'real' directory structure the pattern becomes more and more of a waste. I was thinking I should use a php script by default to check the DB and then use that to check the file structure if it's not found. Does that sound like a better way to go? the!-d should cease to exist once the data has been fully moved.

many thanks,

Andy

jdMorgan

12:15 am on Jun 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you have should work, but either of these is "cleaner" and the second is much more specific.

RewriteCond %{HTTP_REFERER} !^http://(www\.)?a?bc?d
RewriteCond %{HTTP_REFERER}!^http://(www\.)?(abcd¦bcd¦abd)

You should be OK unless someone from "abdicators.com", "abdominals.com", or "bcd-displays.com" wants to hotlink... ;)

I wouldn't worry *too* much about the mod_rewrite file-checking, just avoid it if possible. Comparing that to doing a database lookup on each request, the mod_rewrite check will probably be much faster, unless the database is kept in memory. You might want to work toward a known list of static files or filetypes that *do* exist, rather than using any kind of dynamic checking. This may or may not be a viable solution for your specific site, just an idea... Processing a list in code or memory will always be much faster than going to the filesystem.

Jim

RedAndy

12:32 am on Jun 17, 2005 (gmt 0)

10+ Year Member



Awesome, thanks for helping me Jim - both my code and knowledge have improved. The server will be very happy!

I'll take the risk on bcd-displays.com :-)
(edit: actualy, I might go to go with (a¦bcd\.com¦abd\.com) which looks like the most specific. Thinking: if I permanent redirect the other domains into the main one and non-www into www then I don't need this at all, right? )
I'll see how many static pages are left in couple of weeks and see about a static list,

many thanks

Andy