Forum Moderators: phranque
The url "domain.com/images/style4/item.gif" needs to resolve to "domain.com/home/item/generate.php?style=4". Please notice I am rewriting to an application [T=application/x-httpd-php] because the image returned is dynamic dependent on the referer.
The number (ex.'4') can be anything from 0-99. A seperate question, can you use "\d" instead of "[0-9]"? Is there a difference?
Here is my current version:
RewriteRule images/^style([1-9]¦[1-9][0-9])$/item.gif home/item/generate.php?style=$1 [T=application/x-httpd-php]
Thanks for the help!
"\d" will only work on Apache 2.x which uses PCRE rather than the POSIX regex library used on most Apache 1.3.x servers.
All that said, I'd suggest:
RewriteRule ^images/style([1-9]?[0-9])/item\.gif$ /home/item/generate.php?style=$1 [T=application/x-httpd-php,L]
[edited by: jdMorgan at 3:40 am (utc) on Sep. 26, 2009]
I have a different but related question. In my htaccess file I have it setup to deny hotlinking as so:
#DENY LINKBACKS
SetEnvIfNoCase Referer ^$ local=1
SetEnvIfNoCase Referer ^http://(.+\.)?mydomain\.com local=1
<FilesMatch "\.(zip¦gif¦png¦swf¦jpe?g)$">
Order allow,deny
allow from env=local
</FilesMatch>
How would I go about allowing hotlinking only to the images specified in the RewriteRule we just created? Would I need a seperate htaccess file in that image directory?
I would attempt to construct something myself, but I haven't the foggiest idea where to begin with this one and I can't find anything like it via search.
I corrected the code I posted above so as to avoid propagating that error.
The "^" and "$" characters are regular-expressions start- and end-anchors, respectively, and you should become familiar with their use, as they perform an important role in creating precise regular-expressions patterns.
---
Since "generate.php" is a file, and since it is not one of those filetypes protected by your anti-hotlinking scheme, any images served by your script won't be hotlink-protected unless the script implements that function itself (or mistakenly refers to 'included' image files using a URL instead of a local filepath).
URLs and files are not the same thing. They are associated, but in no way related -- except by the actions of the server. In Apache, files mean files, and URLs mean URLs, and controls of one do not affect the other. Understand that the primary purpose of a server is to map one location system -- Uniform Resource Locators as used 'out on the Web," to another completely-different location system -- the filesystem used by the server's operating system. In this way, user-agents on the Web need not know anything about the server type, the operating system used by that server, or the physical structure of the filesystem on the server.
The use of mod_rewrite makes the distinction quite clear between URLs and files, since code like yours maps a requested .gif URL to a .php script file, obviously breaking any semblance of equivalence between the URL and the file.
---
To avoid unexpected results and confusion due to stale cached objects and server responses, completely flush (delete) your browser cache after any change to your server-side code. Also flush your cache when changing 'roles' between authorized and unauthorized image-linkers while testing, for the same reason.
Jim