I am having a bit of a problem, I am not sure if its an issue with how I'm using mod-rewrite or mod-rewrite isn't behaving as I expect it.
What I have is that when someone hits domain.com/test.gif it redirects to domain.com/script.cgi?test.gif using a rule like:
RewriteRule ^(.+)$ /script.cgi?$1 [PT,L]
(with proper gif only condition before it)
Problem is I don't want people to be able to go to script.cgi?test.gif externally.
There are a lot of resources online that suggest how to do this however none of them work. Ive tried:
RewriteRule ^/script.cgi.* / [F]
this always matches no matter if indirect or direct access so always shows frobidden.
Ive tried using REQUEST_URI and REDIRECT_URL as the CGI script shows that the first contains script.cgi is called directly but not if redirected and REQUEST_URL is blank if called directly. Unfortunately it seems that in Apache these actually have different values at the time of mod_rewrite executing (so they are the same be it a direct call or not).
I also tried some more unconventional things (Setting an ENV variable on the gif->script redirect and then only allowing script.cgi to be called if it is set, however by the time it does the gif->script redirect the env variable is no longer set).
I tried doing
RewriteRule ^script.cgi.* / [F,NS]
Thinking maybe the gif->script redirect was then executing through a sub request (as it isnt an external browser redirect) but no dice it always returns forbidden.
So part of the problem seems to lie that in after the initial match the entire ruleset is being rerun and it is being handled as a totally new request internally and I am not fully sure of why this is happening as from what I read it shouldn't be.
This is on Apache 2.2 for what its worth.
I could implement the REQUEST_URI check in the cgi script as that seems to reflect properly, however I would prefer to solve this with mod_rewrite for security reasons.
Let me know if I am missing anything, or anyone has any ideas. I figure it should be fairly possible to tell if a request was done through mod_rewrite rewrite or not. I know I could also have mod_rewrite append a secret key to the script.cgi and have script.cgi check for that but if someone was able to ever access the rewrite rule or an error message in script.cgi showed its url. Anyway any comments would be appericated:)