httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ian! D. Allen" <idal...@freenet.carleton.ca>
Subject [users@httpd] mod_rewrite fails to remove local directory prefix
Date Fri, 13 Jun 2003 05:35:21 GMT
I've always been led to believe that the pathname accessible in a
RewriteRule placed in a per-directory .htaccess file is the pathname
*from the directory containing the .htaccess file onward*.  I quote from
under the RewriteBase section in the mod_rewrite document:

  http://localhost/manual/mod/mod_rewrite.html
    [...]
    As you will see below, RewriteRule can be used in per-directory
    config files (.htaccess). There it will act locally, i.e., the
    local directory prefix is stripped at this stage of processing and
    your rewriting rules act only on the remainder. At the end it is
    automatically added back to the path.

I've found that the local directory prefix is *not* always stripped.

Is this a documentation bug, a module bug, or have I missed something else?

Let me present my evidence.

These are my configurations (same behaviour for both):

    Mandrake 8.2:

    Server: Apache-AdvancedExtranetServer/1.3.23 (Mandrake Linux/4.2mdk)
    mod_ssl/2.8.7 OpenSSL/0.9.6c DAV/1.0.3 mod_gzip/1.3.19.1a
    FrontPage/5.0.2.2623 mod_auth_external/2.1.14 auth_ldap/1.6.0
    PHP/4.1.2

    Mandrake 9.1:

    Server: Apache-AdvancedExtranetServer/2.0.45 (Mandrake Linux/4.2mdk)
    mod_perl/1.99_08 Perl/v5.8.0 mod_ssl/2.0.45 OpenSSL/0.9.7a PHP/4.3.1

Consider the following directory on machine "idallen.ca":

    /home/idallen/public_html/teaching/

I can get to this directory via the usual tilde syntax:

    http://idallen.ca/~idallen/teaching/

I've also set it up as a named virtual host:

    ServerName teaching.idallen.ca
    DocumentRoot /home/idallen/public_html/teaching

Now there are two ways to get to this directory:

    http://idallen.ca/~idallen/teaching/
    http://teaching.idallen.ca/

(In fact, I also have other virtual hosts and aliases, so the above are
only two of several ways to get to the same directory.)

Consider this code in an .htaccess file in directory public_html/teaching:

    RewriteCond %{HTTP_HOST} !^teaching\.idallen\.ca$
    RewriteRule ^(.*) http://teaching.idallen.ca/$1 [R=permanent,L] 

The intent of the above code is to canonize the host name used to access
the directory.  If the browser gets to this directory using anything
except the "canonical" host name, I want to redirect the browser to use
the canonical "teaching.idallen.ca" host name instead.

I am depending on the accuracy of the manual when it says that the local
directory prefix is stripped - the $1 above must have the local directory
prefix stripped.  The problem is: sometimes it is; sometimes not.

This URL is correctly canonized by the rewrite rule:

    http://idallen.ca/~idallen/teaching/

Evidence:

    $ wget http://idallen.ca/~idallen/teaching/
    HTTP request sent, awaiting response... 301 Moved Permanently
    Location: http://teaching.idallen.ca/

The correct behaviour is also visible in the rewrite log:

    ==> /var/log/httpd/rewrite_log <==
    192.168.0.254 - - [12/Jun/2003:15:07:25 -0400]
       [idallen.ca/sid#80d2d10][rid#808d9d8/initial] (2)
       [per-dir /home/idallen/public_html/teaching/] rewrite  ->
       http://teaching.idallen.ca/
    192.168.0.254 - - [12/Jun/2003:15:07:25 -0400]
       [idallen.ca/sid#80d2d10][rid#808d9d8/initial] (2) [per-dir
       /home/idallen/public_html/teaching/] explicitly forcing redirect
       with http://teaching.idallen.ca/
    192.168.0.254 - - [12/Jun/2003:15:07:25 -0400]
       [idallen.ca/sid#80d2d10][rid#808d9d8/initial] (1)
       [per-dir /home/idallen/public_html/teaching/] escaping
       http://teaching.idallen.ca/ for redirect
    192.168.0.254 - - [12/Jun/2003:15:07:25 -0400]
       [idallen.ca/sid#80d2d10][rid#808d9d8/initial] (1)
       [per-dir /home/idallen/public_html/teaching/] redirect to
       http://teaching.idallen.ca/ [REDIRECT/301]

Note the very first rewrite: rewrite  -> http://teaching.idallen.ca/

The above evidence shows that the local directory prefix
"/home/idallen/public_html/teaching" has been correctly stripped
by mod_rewrite (leaving no characters in $1).  This is good.


This next example below does not work.  I simply remove the trailing
slash from the URL I gave above, and I have a URL that fails:

    http://idallen.ca/~idallen/teaching

Evidence:

    $ wget http://idallen.ca/~idallen/teaching
    HTTP request sent, awaiting response... 301 Moved Permanently
    Location: http://teaching.idallen.ca//home/idallen/public_html/teaching
    15:12:05 ERROR 404: Not Found.

Wrong!  The local directory prefix hasn't been removed from file name
seen by the .htaccess file.  The entire absolute pathname is being
picked up and appended to the canonized server host name, contrary to
the documentation.

The incorrect behaviour is visible in the rewrite log:

    ==> /var/log/httpd/rewrite_log <==
    192.168.0.254 - - [12/Jun/2003:15:12:05 -0400]
       [idallen.ca/sid#80d2d10][rid#808d9d8/initial] (2)
       [per-dir /home/idallen/public_html/teaching/]
       rewrite /home/idallen/public_html/teaching ->
       http://teaching.idallen.ca//home/idallen/public_html/teaching
    192.168.0.254 - - [12/Jun/2003:15:12:05 -0400]
       [idallen.ca/sid#80d2d10][rid#808d9d8/initial] (2) [per-dir
       /home/idallen/public_html/teaching/] explicitly forcing redirect with
       http://teaching.idallen.ca//home/idallen/public_html/teaching
    192.168.0.254 - - [12/Jun/2003:15:12:05 -0400]
       [idallen.ca/sid#80d2d10][rid#808d9d8/initial] (1)
       [per-dir /home/idallen/public_html/teaching/] escaping
       http://teaching.idallen.ca//home/idallen/public_html/teaching for
       redirect
    192.168.0.254 - - [12/Jun/2003:15:12:05 -0400]
       [idallen.ca/sid#80d2d10][rid#808d9d8/initial] (1)
       [per-dir /home/idallen/public_html/teaching/] redirect to
       http://teaching.idallen.ca//home/idallen/public_html/teaching
       [REDIRECT/301]

Note the very first rewrite above:

   rewrite /home/idallen/public_html/teaching ->
       http://teaching.idallen.ca//home/idallen/public_html/teaching

The local directory prefix has not been removed from the file name seen
by the .htaccess file.  That "/home/idallen/public_html/teaching" should
be "", as it was in the previous example that worked correctly.

I have a kludge work-around, which is to put something similar to this
at the start of the .htaccess file:

    RewriteRule ^/home/idallen/public_html/ %{REQUEST_URI}/ [R=permanent,L] 

This tells the browser to re-issue the request with a trailing slash,
if the local directory prefix has not been properly removed.  It turns
a request that breaks the module into one that works.

The local directory prefix is not being removed.
Is this a documentation bug, a module bug, or have I missed something?

-- 
-IAN!  Ian! D. Allen   Ottawa, Ontario, Canada
       EMail: idallen@idallen.ca   WWW: http://www.idallen.com/
       College professor via: http://teaching.idallen.com/
       Board Member, TeleCommunities CANADA  http://www.tc.ca/

---------------------------------------------------------------------
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