Search code examples
mod-rewriteurl-rewritingcoldfusionapache2lucee

mod_rewrite is sending multiple requests back to Lucee server


I seem to have a problem whereby mod_rewrite is sending multiple requests back to my Lucee server. Sometimes 2 requests, sometimes 4 requests. I have a special application framework in Lucee to handle and manage all requests. It requires the request to be rewritten and proxied back to Lucee as "/" (root) with a request parameter set to the user requested URI. All of the rewriting appears to work properly but more than one request gets fired by the backend server. The following is my rewrite rule setup:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)$ "/" [E=vPath:$1,PT,NE,NS,L]

<If "-T reqenv('vPath')">
    RequestHeader add "vPath" "%{vPath}e"
</If>

The RewriteRule simply grabs the URI and sets the environment variable "vPath", then rewrites the URI to "/" with the parameters:

PT (pass through to next handler)
NE (no URI escaping of output)
NS (not for internal sub-requests)
L (last rule)

This all seems to work just as needed, however, in testing I have verified that the request hitting the code behind in Lucee is actually getting called multiple times. If I turn RewriteEngine off, then only a single request is sent to the backend. Turn RewriteEngine on and multiple requests are again sent (anywhere from 2 to 6).

Also note that it doesn't seem to matter if I use "P" (force proxy) or "PT" (pass through to next handler), I get the same multiple request results.

Any help would be greatly appreciated. I am somewhat of a NUBE to mod_rewrite.

System:

OS: Linux 4.18.0-11-generic
Java: 1.8.0_181
Lucee: 5.2.9.31
Apache Tomcat: 8.5.33

Solution

  • Alex was correct in referring me to utilizing the logging for mod_rewrite. [hat tip to Alex, thanks!]

    Take note that for mod_rewrite v2.4 and above, you simply use

    LogLevel alert rewrite:trace6
    

    And this will update the log file in

    /var/log/apache2/error.log
    

    My problem was that I was not accounting for other requests to files that don't actually exist that were also being redirected to the web root, like

    /favicon.ico
    

    The lesson here is to be certain to account for all requests being proxied back to the application server, otherwise it will simply execute the root request for each URI that is directed to the application.