httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David S. Madole" <da...@madole.net>
Subject [users@httpd] mod_rewrite per-dir trailing slash problem
Date Thu, 11 Nov 2004 20:10:43 GMT
I seem to have encountered a problem with mod_rewrite following an
upgrade from 2.0.50 to 2.0.52.

I have many .htaccess files similar to the following:

RewriteEngine on
RewriteBase /test
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{SERVER_NAME}/test/$1 [redirect]

In this case, the purpose is to redirect the user to an SSL version of
the URI.

The problem that I find is that this does not work properly for URIs that
reference the directory itself without the trailing slash. It appears
that this was not handled correctly by mod_rewrite on 2.0.50 either, but
it seems that mod_dir did a redirect before the URI ever got to
mod_rewrite and so it didn't matter.

What happens is that the RewriteBase does not match the URI and so does
not get stripped before the RewriteRules are processed. Here's the
rewrite log output for this example:

66.212.201.164 - - [11/Nov/2004:14:41:37 --0500]
[www.madole.net/sid#811dd80][rid#81ec050/initial] (3) [per-dir
/home/madole.net/www/test/] applying pattern '(.*)' to uri
'/home/madole.net/www/test'
66.212.201.164 - - [11/Nov/2004:14:41:37 --0500]
[www.madole.net/sid#811dd80][rid#81ec050/initial] (2) [per-dir
/home/madole.net/www/test/] rewrite /home/madole.net/www/test ->
https://www.madole.net/test//home/madole.net/www/test
66.212.201.164 - - [11/Nov/2004:14:41:37 --0500]
[www.madole.net/sid#811dd80][rid#81ec050/initial] (2) [per-dir
/home/madole.net/www/test/] explicitly forcing redirect with
https://www.madole.net/test//home/madole.net/www/test
66.212.201.164 - - [11/Nov/2004:14:41:37 --0500]
[www.madole.net/sid#811dd80][rid#81ec050/initial] (2) [per-dir
/home/madole.net/www/test/] trying to replace prefix
/home/madole.net/www/test/ with /test
66.212.201.164 - - [11/Nov/2004:14:41:37 --0500]
[www.madole.net/sid#811dd80][rid#81ec050/initial] (1) [per-dir
/home/madole.net/www/test/] escaping
https://www.madole.net/test//home/madole.net/www/test for redirect
66.212.201.164 - - [11/Nov/2004:14:41:37 --0500]
[www.madole.net/sid#811dd80][rid#81ec050/initial] (1) [per-dir
/home/madole.net/www/test/] redirect to
https://www.madole.net/test//home/madole.net/www/test [REDIRECT/302]

The problem seems to be in the following test at line 1980 in
mod_rewrite.c:

        if (   !is_proxyreq && strlen(uri) >= strlen(perdir)
            && strncmp(uri, perdir, strlen(perdir)) == 0) {
            rewritelog(r, 3, "[per-dir %s] strip per-dir prefix: %s ->
%s",
                       perdir, uri, uri+strlen(perdir));
            uri = uri+strlen(perdir);
        }

I have changed the test as follows to cause the match to occur and the
prefix to be stripped even when the URI is missing the trailing slash:

--- mod_rewrite.c.orig  Fri Aug 27 15:23:26 2004
+++ mod_rewrite.c       Thu Nov 11 01:58:43 2004
@@ -1977,11 +1977,14 @@
            is_proxyreq = (   r->proxyreq && r->filename
                           && !strncmp(r->filename, "proxy:", 6));

-        if (   !is_proxyreq && strlen(uri) >= strlen(perdir)
-            && strncmp(uri, perdir, strlen(perdir)) == 0) {
+       i = strlen(perdir) - 1;
+
+        if (   !is_proxyreq && strlen(uri) >= i
+            && strncmp(uri, perdir, i) == 0
+           && uri[i] == '\0' || uri[i] == '/') {
             rewritelog(r, 3, "[per-dir %s] strip per-dir prefix: %s ->
%s",
                        perdir, uri, uri+strlen(perdir));
-            uri = uri+strlen(perdir);
+            uri = uri + i + 1;
         }
     }

This solves the problem, but I wonder if I'm missing something else in
the bigger picture. It seems a little too obvious a problem to exist this
long, but maybe it was just masked by changes in mod_dir. I'd appreciate
any opinions.

Thanks,
David


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Mime
View raw message