I'm building a site that needs to include a 'check' procedure, to do several initiation tasks for a user's session. Examples include checking whether they're accepting cookies, determining if their IP address grants them specific privileges, etc.
Once the check is complete, I need to redirect the user back to the page they originally requested.
The plan is to use RewriteCond
and map all URLs to an 'initiator' if the user doesn't have a specific cookie set.
Let's say I want to rewrite all URLs (ultimately, with some conditions, of course) to:
/foo?original_url=...
Where the ...
is the original URL requested, URL-encoded.
The closest I've got is this:
RewriteRule ^(.*)$ http://localhost/php/cookie.php$1 [R=301]
I can then inspect the original URL, captured in the backreference, via PATH_INFO
. However, this is pretty messy - I would much prefer to pass that value as a URL parameter
see http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa, which covers this exact use-case.
In short, use:
The
B
should re-escape the original path.Alternatively, if you don't redirect, but rather use the FallbackResource directive, you can then access the original requested URI in the env var REQUEST_URI
This has the additional advantage of avoiding the necessity to do explicit RewriteCond's to avoid other valid resources (images, css, and other things you want to be handled normally.)
See http://httpd.apache.org/docs/current/mod/mod_dir.html#fallbackresource for details.