Thursday, February 3, 2011

Mod-Rewrite loading files behind the DocumentRoot

I'm using htaccess and mod_rewrite to point to files that reside behide the DocumentRoot. My folder structure looks like this:

    home/
        webroot/
        other_files/

I have a .htaccess file in webroot with the following:

RewriteEngine on
RewriteRule ^(.*)$ /home/other_files/$1

If I try and access http://mysite.com/file.html I am receive the following error:

The requested URL /home/other_files/file.html was not found on this server.

Is it even possible to load files that are behind the DocumentRoot? If so, can someone point me in the right direction? Thanks in advance.

  • I believe you need to add a section with

    <Directory "/home/other_files">
      (options)
    </Directory>
    

    to your server configuration before apache will be able to serve anything from it. For an example, my DocumentRoot is /var/www but there is this section in the default available site:

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
    

    You could then rewrite a URL to go to /doc/ and the server would know where to get the files from.

    Gabriel Sosa : nice answer ! thank you
  • Just so you know why that rule doesn't work:

    The reason that it isn't able to rewrite to /home/other_files/file.html is that mod_rewrite is parsing the path as /home/webroot/home/other_files/file.html since from mod_rewrite's point of view the preceding slash is equivelant to your document root of /home/webroot.

    Ryan Ahearn's suggestion is a decent one, and is likely the route you want to go.

    From akdom
  • Thanks Ryan and akdom, I'll look into your suggestion Ryan.

0 comments:

Post a Comment