URI escaping in SPARQL query in Recipe 6

Raised by:
Diego Berrueta
Opened on:
Escape sequences in URIs (such as %20 for whitespaces) must be double-escaped to
build a SPARQL query in the implementation of Recipe 6, pattern 2. The issue and
a potential solution were raised by Josh Tauberer (2008-01-20):

I ran into a problem when I created some URIs with %20's in them, 
because the redirect would need to double-escape the %20's when they are 
put into the query string.

After some chin-scratching I found out that mod_rewrite could be used to 
do a proper redirect, and I've documented it here:

There's more explanation in the link, but the short story is putting 
into the main httpd.conf:

   RewriteMap esc int:escape

and then into .htaccess:

   RewriteEngine on
   RewriteBase "/"
   RewriteRule ^(rdf/.*) http://%{HTTP_HOST}/sparql?   (..all one line..)
               query=DESCRIBE+<http://%{HTTP_HOST}/${esc:$1}> [R]


# When we get URIs with %20s or other escaped characters in them,
# a simple RedirectMatch won't do because in the query string
# DESCRIBE query, the escaped characters will be unescaped at
# some point during processing. So this is bad:
# RedirectMatch 303 (/rdf/.*)$1%3E
# Instead, we need to double-escape the characters: The percent
# signs should ultimately be escaped so the processor gets back
# the original escaping when unescaping is applied.
# To do this, we need to use mod_rewrite. However, mod_rewrite is
# operating on the unescaped URI, so we need the 'escape' mapping
# function, which needs to be activated in httpd.conf with:
#     RewriteMap esc int:escape
# The rewrite rule below re-escapes the unescaped URI (getting back
# the problematic URL we started with), and then mod_rewrite
# escapes it again when it sends the redirect, finally achieving
# the double-escape.

RewriteEngine on
RewriteBase "/"
RewriteRule ^(rdf/.*)
http://%{HTTP_HOST}/sparql?query=DESCRIBE+<http://%{HTTP_HOST}/${esc:$1}> [R]

Related emails:
  1. ISSUE-98: URI escaping in SPARQL query in Recipe 6 (from on 2008-03-16)
  2. Re: [Recipes] Open issues in Recipes (from on 2008-10-04)
  3. Re: [Recipes] Open issues in Recipes (from on 2008-10-09)
  4. [Recipes] proposed resolution for remaing issues (from on 2008-11-03)
  5. Re: [Recipes] proposed resolution for remaing issues (from on 2008-11-19)

Related notes:

2008-11-19: Postponed as per [[Maybe we should have added a link to this issue in the Recipes, but we didn't. The problem is real, although it won't be noticed by most of our readers. I haven't tested the proposed solution in detail, and I vaguely remember Ralph raising some concern about it during a telecon many months ago. Implementing the solution "by default" in all the recipes would obfuscate them. I think postponing the issue is the best solution: we acknowledge the problem, we provide the solution, and we can include a pointer in a future revision of the Recipes.]] Decided in