Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mod-Rewrite loading files behind the DocumentRoot

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

home/
    webroot/
    other_files/

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

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

If I try to access http://example.com/file.html I 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?

like image 222
Chad Paulson Avatar asked Aug 12 '08 03:08

Chad Paulson


People also ask

What does IfModule mod_rewrite C mean?

The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.

What does a2enmod rewrite do?

a2enmod = Apache2 Enabled ModThe a2enmod command actually moves the Apache module files from /etc/apache2/mods-available to /etc/apache2/mods-enabled. The second method is to manually move the module files ourselves.

How do I rewrite an Apache rule?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

How do I enable rewrite mod?

In order for Apache to understand rewrite rules, we first need to activate mod_rewrite . It's already installed, but it's disabled on a default Apache installation. Use the a2enmod command to enable the module: sudo a2enmod rewrite.


3 Answers

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 equivalent to your document root of /home/webroot.

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

like image 72
akdom Avatar answered Oct 10 '22 04:10

akdom


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.

like image 24
Ryan Ahearn Avatar answered Oct 10 '22 05:10

Ryan Ahearn


The credit goes to Ryan Aheam, but I'm going to spell it out. I'm a beginner and even with Ryan's answer I had to experiment with a few things to get the syntax right.

I wanted my DocumentRoot to be my cakephp directory. But then I had a Mantis Bug tracker that was just regular PHP and so not in the cakephp directory. The the files below I have the following working.

http://www.example.com : served by /var/www/cakephp

http://www.example.com/mantisbt : served by /var/www/html/mantisbt

File /etc/httpd/conf/httpd.conf

Alias /mantisbt/ "/var/www/html/mantisbt/"                                                                          
<Directory "/var/www/html/">                                                                                        
    AllowOverride All                                                                                               
</Directory>                                                                                                        
                                                                                                                    
<VirtualHost *:80>                                                                                                  
    ServerAdmin me@my_email.com                                                                             
    DocumentRoot /var/www/cakephp                                                                                   
    ServerName my_website.com                                                                                      
    <Directory /var/www/cakephp/>                                                                                   
        AllowOverride All                                                                                           
    </Directory>                                                                                                    
</VirtualHost>

File /var/www/cakephp/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^mantisbt/?$   /mantisbt/  [NC,L]
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>
like image 38
Joe C Avatar answered Oct 10 '22 05:10

Joe C