You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by simran <si...@cse.unsw.edu.au> on 2002/05/17 09:59:36 UTC

Dynamically Changing the Document Root

Hi All, 

We are currently building a mod_perl application that has a content
management component in it. 

After editing a page, we want to be able to give people a 'Preview your
site' ability where they are view a 'preview/staged' mode of their site.

The 'staged' version of their site is stored in a different directory,
and if i could just changed the document_root dynamically, most(?) of
our issues would be over. 

Is there a way to do this? The documentation does not have any reference
to be able to change the document root (just get it via
$r->document_root). 

simran.

ps: We are intending on having something like where when people go to
    http://www.testsite.com/preview/contactus/index.html we strip out
    the "/preview" from the URL (i assume i can change this via
    $r->uri... and change the document root from 
    /home/www/testsite.com/current to /home/www/testsite.com/stage

Re: Dynamically Changing the Document Root

Posted by Marc Slagle <ma...@fulkertconsulting.com>.
> The 'staged' version of their site is stored in a different directory,
> and if i could just changed the document_root dynamically, most(?) of
> our issues would be over.
>
> Is there a way to do this? The documentation does not have any reference
> to be able to change the document root (just get it via
> $r->document_root).

You might want to use a translation handler (Chapter 7 of the Eagle book.)

We did something similar to this, where if a browser asks for an index page
it forces apache to use a db file to get its filename/path.  You could check
the incoming path for the "Preview" mode and set $r->filename.

Something like:

my $uri=$r->parsed_uri;
my $Path=$uri->path;

if ($Path =~ /Preview/)
{
    $r->filename($Path_to_file_on_filesystem);
    return OK;
}

return DECLINED;

The above might not exactly do it, but its just a rough example.  The return
OK will make apache skip its own URL to file translation stuff, where return
DECLINED should let apache continue as normal (reading httpd.conf for
finding paths, etc.)

Marc Slagle





Re: Dynamically Changing the Document Root

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
 
> Is there a way to do this? The documentation does not have any reference
> to be able to change the document root (just get it via
> $r->document_root). 


see recipe 4.3 in the cookbook for a description of $r->document_root 
and the perils involved.  the minimum you should be aware of is that 
DocumentRoot is a server attribute, not a request attribute, so 
changing it affects all future requests to the child process unless 
you set it back.

the code we use as an example is here

   http://www.modperlcookbook.org/code/ch04/Cookbook/Userdir.pm

and all of chapter 4 can be viewed online here

   http://www.webreference.com/programming/perl/cookbook/

HTH

--Geoff


Re: Dynamically Changing the Document Root

Posted by Per Einar Ellefsen <pe...@skynet.be>.
At 09:59 17.05.2002, simran wrote:
>ps: We are intending on having something like where when people go to
>     http://www.testsite.com/preview/contactus/index.html we strip out
>     the "/preview" from the URL (i assume i can change this via
>     $r->uri... and change the document root from
>     /home/www/testsite.com/current to /home/www/testsite.com/stage

You can't change the document root, but you don't necessarily need to 
either. You could set up an alias in httpd.conf
<VirtualHost foo>
Alias /preview/ /home/www/testsite.com/stage/
:
</VirtualHost>

Or you could use a generic URI translation handler which would do the URI 
translation, running in /preview
<Location /preview>
PerlTransHandler My::Foo
</Location>
then My::Foo maps the URI to a filename or something like that.


-- 
Per Einar Ellefsen
per.einar@skynet.be