Trick for using back-references in match patterns?

Discuss practical ways rearrange URLs using mod_rewrite.

Trick for using back-references in match patterns?

Postby vanillabean » Mon Jun 01, 2009 3:09 am

Hi,

I'm wondering if anybody knows a trick to effectively use a back-reference in a match pattern?

Here's an example of what I mean.

Code: Select all
RewriteEngine on
RewriteMap unescape int:unescape

# Puts a user_id in back-reference %1
RewriteCond ${unescape:%{HTTP_COOKIE}} userid=<(.*)> [NC]

# Puts a report_id in back-reference %2
RewriteCond %{REQUEST_URI} member/reports/(\d+)\.pdf$ [NC]

# Checks if a custom-generated file exists
RewriteCond %{DOCUMENT_ROOT}/members/tagged_reports/%1_%2\.pdf -f [NC]

# Checks if a "report" cookie exists and puts
# a report_id in back-reference %3
RewriteCond %{HTTP_COOKIE} report=(\d+) [NC]

# Now I want to check if a cookie value exists
# with the same value of the report id that's being
# requested --- But I'm not sure how to do this.
# NOTE: I think this doesn't work because %3 isn't a PCRE pattern
RewriteCond %2=%3

# RewriteCond %2 %3

# This rule rewrites the request to a URL that returns a document customized for the user.
RewriteRule ^member/reports/(\d+)\.pdf$ /members/tagged_reports/%1_%2.pdf [NC,L]


Thanks for any suggestions.
vanillabean
 
Posts: 5
Joined: Fri May 29, 2009 12:04 am

Postby vanillabean » Mon Jun 01, 2009 4:14 am

I found a way to do this using REGEX back-references, something like this:

# means if %3 = %4
RewriteCond %3:%4 (\d+):\1

and

# means if %3 != %4
RewriteCond %3:%4 !(\d+):\1

Thanks
vanillabean
 
Posts: 5
Joined: Fri May 29, 2009 12:04 am

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

You should use
Code: Select all
^(\d+):\1$

or %3=123 would match %4=1234.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 30 guests

cron