You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by David Pellegrini <da...@datawebsystems.com> on 2002/04/17 01:31:26 UTC

Use of env vars in httpd.conf wrt mod_rewrite

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


Re: Use of env vars in httpd.conf wrt mod_rewrite

Posted by Joshua Slive <jo...@slive.ca>.
On Tue, 16 Apr 2002, David Pellegrini wrote:
> Still, now I'm curious how to reference env vars within httpd.conf.
> http://httpd.apache.org/docs/env.html doesn't address it.

Can't be done.  httpd.conf is a configuration file, not a programming
language.  There are some modules floating around that let you do some
variable substitution in httpd.conf, but my advice is simply to
pre-process the file using m4 or your favorite language if you need
something complex.

Joshua.


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


Re: Use of env vars in httpd.conf wrt mod_rewrite

Posted by David Pellegrini <da...@datawebsystems.com>.
Joshua Slive wrote:
> 
> On Tue, 16 Apr 2002, David Pellegrini wrote:
> >
> > 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:
> >
> 
> Ummm... What's wrong with the DOCUMENT_ROOT environment variable that
> is referenced here:
> http://httpd.apache.org/docs/mod/mod_rewrite.html#RewriteCond
> 
> Joshua.

I was getting the default document root, not the virtual host's.  I just now
tried it again and it worked, so I must've screwed up the vhost definition
somehow when I tried it earlier.  I'm so embarrassed.

Still, now I'm curious how to reference env vars within httpd.conf.
http://httpd.apache.org/docs/env.html doesn't address it.

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


Re: Use of env vars in httpd.conf wrt mod_rewrite

Posted by Joshua Slive <jo...@slive.ca>.
On Tue, 16 Apr 2002, David Pellegrini wrote:
>
> 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:
>

Ummm... What's wrong with the DOCUMENT_ROOT environment variable that
is referenced here:
http://httpd.apache.org/docs/mod/mod_rewrite.html#RewriteCond

Joshua.


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