Can I use mod_rewrite to inject additional requests?

Discuss practical ways rearrange URLs using mod_rewrite.

Can I use mod_rewrite to inject additional requests?

Postby vanillabean » Fri May 29, 2009 12:31 am

I'm wondering if mod_rewrite is the right tool for this problem. I'd appreciate any suggestions.

I have urls that look like this: /reports/12345.pdf
I have rules that rewrite those urls to /watermark_pdf.cfm?id=12345
which uses some session info to stamp a user's identification on a PDF, saves it in a temp directory, and redirects back to /reports/12345.pdf

I also have a rule that checks whether a previously watermarked file exists in the temp dir, and if it does rewrites the url to that file.

All this is working fine.

Now I have an additional requirement to perform some custom logging whenever a watermarked file is served. I can't add this to the code that does the watermarking, it needs to be "injected" somehow only when the file is served, since we want to keep track of stats like how many times these files are downloaded.

Is there any way that I can use mod_rewrite to "inject" a private request ahead of the actual request? The private request would perform the custom logging, and when it finishes, I would want to rewrite engine to carry on with rewriting the rule.

When I naively tried this by simply not specifying the [L] last flag on the rule that rewrites the URL to do the logging, the logging is performed, but I can't get the rewrite engine to carry on processing with the next rule which is supposed to actually return the content.

Thanks for any help in clarifying if what I'm trying to do here is possible or not.
vanillabean
 
Posts: 5
Joined: Fri May 29, 2009 12:04 am

Postby richardk » Fri May 29, 2009 8:58 am

As soon as control is passed to a script (the coldfusion actually running) mod_rewrite can't process the request any more.

If you have access to the httpd.conf file you could use a RewriteMap.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby vanillabean » Mon Jun 01, 2009 2:30 am

Thanks for your reply.

That's what I had suspected, and it does makes sense.

RewriteMap does seem like the perfect solution to this, but unfortunately I'm using isapi_rewrite which doesn't support MapType: prg.

I'll have to search for a different solution.
vanillabean
 
Posts: 5
Joined: Fri May 29, 2009 12:04 am

Postby richardk » Tue Jun 02, 2009 11:04 am

You could have watermark_pdf.cfm send the file instead of redirecting or you could redirect to a slightly different URL.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby vanillabean » Tue Jun 02, 2009 8:19 pm

Actually, the reason I can't have watermark_pdf.cfm send back the file is the PDF is "linearized" which basically means clients (ie. PDF readers, browser plugins) can request the PDF in chunks (so that the first page of the PDF can be displayed before the entire PDF is downloaded). This results in multiple requests to the webserver for these chunks. I want the webserver to handle these chunking details, not the CFM page.

Another requirement is that the original URL remain the same from the client's perspective (hence using mod_rewrite) for bookmarking purposes.

I got this working using 3 rewrite rules and cookies.

Just in case anyone's interested here are the rules:

Code: Select all
RewriteEngine on

RewriteMap unescape int:unescape


# These rules are used to watermark, log access to and download PDF reports.

# ---------------------------------
# Generated the watermarked file if it doesn't already exist
# ---------------------------------
RewriteCond ${unescape:%{HTTP_COOKIE}} userid=(.*) [NC]
RewriteCond %{REQUEST_URI} member/reports/(\d+)\.pdf$ [NC]
RewriteCond %{DOCUMENT_ROOT}/members/tagged_reports/%1_%2\.pdf !-f [NC]

# watermark_pdf.cfm will perform an internal redirect
# back to the original URL (i.e. /member/reports/%2.pdf)
RewriteRule ^member/reports/(\d+)\.pdf$ /member/tagging/watermark_pdf.cfm?article_id=%2 [NC,L]



# ---------------------------------
# Log Access to watermarked file
# ---------------------------------
RewriteCond ${unescape:%{HTTP_COOKIE}} userid=(.*)    [NC]
RewriteCond %{REQUEST_URI} member/reports/(\d+)\.pdf$ [NC]

# If no report cookie exists OR if the report cookie
# a value different from the report being requested this is
# the signal to log access to the report.
RewriteCond %{HTTP_COOKIE} !report=.                  [NC, OR]
RewriteCond %{HTTP_COOKIE} report=(\d+)               [NC, OR]
RewriteCond %2:%3 !(\d+):\1                                       
RewriteCond %{DOCUMENT_ROOT}/members/tagged_reports/%1_%2\.pdf -f [NC]

# creates a cookie with a lifespan of 2-minutes,
# named report=xxxx, where xxxx is the id of the
# report being requested.

# log.cfm will perform an internal redirect
# back to the original URL (i.e. /member/reports/%2.pdf)
RewriteRule ^member/reports/(\d+)\.pdf$ /member/tagging/log.cfm?article_id=%2 [NC,L,CO=report:%2:.my_domain.com:2:/]



# ---------------------------------
# Download the watermarked file
# ---------------------------------
RewriteCond ${unescape:%{HTTP_COOKIE}} userid=(.*)    [NC]
RewriteCond %{REQUEST_URI} member/reports/(\d+)\.pdf$ [NC]
RewriteCond %{HTTP_COOKIE} report=(\d+)               [NC]

# If the watermarked file exists
RewriteCond %{DOCUMENT_ROOT}/members/tagged_reports/%1_%2\.pdf -f [NC]

# if access to the report being requested has been logged
# (signalled by the report cookie value matching the request report id)
RewriteCond %3:%4 (\d+):\1
RewriteRule ^member/reports/(\d+)\.pdf$ /members/tagged_reports/%1_%2.pdf [NC,L]
vanillabean
 
Posts: 5
Joined: Fri May 29, 2009 12:04 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 25 guests

cron