You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by andrea rossignoli <ap...@libero.it> on 2003/09/30 14:33:07 UTC

Added an env var to mod_rewrite

Hi,

in some situations it could be good to read the value on the internal 
redirect counter of mod_rewrite.

Take a look at this scenario:

/.htaccess
----
RewriteEngine On

# If there are not internal redirects
# ( e.g. the redirects counter is zero )
# then denies accesses to secret/ subdirectory
RewriteCond %{REWRITE_REDIRECTS} ^0$
RewriteRule ^secret/ - [F,L]

# Internally redirect to secret script
RewriteRule ^something$ /secret/order.php
----

THe accesses to secret subdirectory are denied,
except when an internal redirection is done.

OKay, maybe the example is not self-good.

I took a look at mod_rewrite.c (release 1.3.28 for win32),
and to create an enviroment variable called ``REWRITE_REDIRECTS''
I did:



@@ -1681,6 +1681,30 @@
      return (reqc->redirects++ >= reqc->redirect_limit);
  }

+static int get_redirect_counter(request_rec *r)
+{
+    request_rec *top = r;
+    rewrite_request_conf *reqc;
+
+    while (top->main) {
+        top = top->main;
+    }
+    while (top->prev) {
+        top = top->prev;
+    }
+
+    /* fetch our config */
+    reqc = (rewrite_request_conf *) 
ap_get_module_config(top->request_config,
+                                                         &rewrite_module);
+
+    /* no config there? returns 0 */
+    if (!reqc) {
+        return 0;
+    }
+
+    return (reqc->redirects);
+}
+

  /*
  ** +-------------------------------------------------------+
@@ -3755,6 +3779,11 @@
                      MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
          result = resultbuf;
      }
+
+    else if (strcasecmp(var, "REWRITE_REDIRECTS") == 0) {
+        ap_snprintf(resultbuf, sizeof(resultbuf), "%d", 
get_redirect_counter(r));
+        result = resultbuf;
+    }

      /* underlaying Unix system stuff */
      else if (strcasecmp(var, "TIME_YEAR") == 0) {
@@ -4509,3 +4538,4 @@
  }



What do you think ?


THanks,
Mr Andrea Rossignoli