0

Edit: To pose the same question in a more general way: how can I rewrite a URL in htaccess so that the rewritten form will be seen in php's $_SERVER['REQUEST_URI']?


If user requests an (existing) epub or mobi file via a URL, I want to capture and redirect the request so that the Flight php router will handle it via one of its routes. (To keep a count of downloads.) So, the request for

http://example.com/book.epub

should be rewritten to

http://example.com/download/epub

and this should then be fed to the router, as if it were the original request. Here is what does not work:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # not using [L] flag, so that the modified request gets handled later
    # for book.epub this rule should produce /download/epub etc.
    RewriteRule ^book\.(epub|mobi)$ /download/$1 [QSA]

    # Flight rewrite rules
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Problem #1 is that the test for existing REQUEST_FILENAME still succeeds (it checks against the original request, not the rewritten form). But the test must stay, of course, so that requests for images, css and other regular "assets" don't get forwarded to the router.

However, there is also problem #2: even if the requested file does not physically exist, Flight still does not get to see the rewritten URL. Internally, it looks at $_SERVER['REQUEST_URI'], so it does not get to see the rewritten form of the request. Is what I am trying to achieve even possible? (Perhaps with a different router?)

Marek Jedliński
  • 7,088
  • 11
  • 47
  • 57

1 Answers1

0

FWIW, I had to use a catch-all route in php and intercept those requests there as a "special case".

For some reason I expected to see the rewritten URL in one of the $_SERVER fields, but it is not available there.

Marek Jedliński
  • 7,088
  • 11
  • 47
  • 57