httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Pellegrini <dav...@datawebsystems.com>
Subject Use of env vars in httpd.conf wrt mod_rewrite
Date Tue, 16 Apr 2002 23:31:26 GMT
I searched the FAQ and skimmed the last four months of the mailing list
archives, and did not find the answer to this question, so I'm posting to the
list in hopes that someone can help while I keep searching.

Background:
I'm setting up SSI and some rewrite rules so that all requests for static html
pages get served via a standard page template.  This allows my content
developers to write very simple html without having to worry about overall page
layout, as well as improving modularity, reuse, ripple effect of changes to the
standard layout, etc, etc, Mom, and apple pie.  I've got all that working great
with just a couple of directives.  In httpd.conf I have a rewrite rule to route
html requests to my template, passing the requested file as an env var:

    RewriteRule   (.*\.html)        /template.shtml [NS,E=FileName:$1]

template.shtml has all of the header, footer and navbar html.  Then the "body of
the page" is included thusly:

    <!--#include virtual="$FileName" -->

Simple and elegant -- just the way I like it.


Problem:
I want to check first that the requested file exists before doing the magic to
wrap the file in the standard page template.  I want to return 404 rather than
displaying the page template with "[an error occurred while processing this
directive]" (the result of "#include virtual" looking for a non-existent file).

My first attempt used RewriteCond to check with -f

    RewriteCond   %{REQUEST_FILENAME}  -f
    RewriteRule   (.*\.html)           /template.shtml [NS,E=FileName:$1]

However, it appears that the RewriteCond is evaluated without the benefit of the
DocumentRoot appended at the beginning.  I can see evidence of that in the
rewrite log file.  It always fails to match.

Most of the apache doc examples I've reviewed includes the document hardcoded
into the ReWriteCond record like this:

    RewriteCond    /my/document/root/path/%{REQUEST_FILENAME}  -f

This works, but it's ugly, and repeats information I've already hardcoded in the
DocumentRoot directive.  If I move my document root, I have to remember to
update my RewriteCond's as well (and what are the chances of that happening the
first time?).  Ideally I'd just dereference the value of DocumentRoot in my
RewriteCond record, but I don't know how to do that (or if it's possible).
So that's question #1.  As an alternative, I tried using SetEnv to establish the
document root, then dereference it in the DocumentRoot and RewriteCond records,
but that doesn't work.  Example:

<VirtualHost 192.168.1.1>
    ServerName        vhost1.domain.com
    SetEnv vhost1Root /my/document/root/path
    DocumentRoot      %{vhost1Root}
    Options           Includes
    RewriteEngine     on
    RewriteLog        /var/log/httpd/vhost1-rewrite.log
    RewriteLogLevel   9
    RewriteCond       %{ENV:vhost1Root }/%{REQUEST_FILENAME}  -f
    RewriteRule       (.*\.html)        /template.shtml [NS,E=FileName:$1]
</VirtualHost>

This does not work.  First, no substitution occurs in the DocumentRoot record. 
Second, the %{ENV:DevelopmentRoot} in the RewriteCond record evaluates to the
empty string.  What is the magic incantation?  How do I dereference an env var
within httpd.conf?  Is what I'm attempting even possible?  Seems like it ought
to be, but I've scoured the apache documentation and found nothing definitive.
I've seen usage of SetEnvIf where the value of the env var is used later in
httpd.conf, so I'm hopeful I'm on the right track, but right now I'm stymied. 
Any insight into a solution would be much appreciated.  What's the "right" way
to go about this?

Thanks!

-davidp

---------------------------------------------------------------------
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
For additional commands, e-mail: users-help@httpd.apache.org


Mime
View raw message