2008-08-12 25 views
12

我使用.htaccessmod_rewrite指向驻留在DocumentRoot后面的文件。我的文件夹结构如下所示:Mod-Rewrite在DocumentRoot后加载文件

home/ 
    webroot/ 
    other_files/ 

我有一个.htaccess文件根目录,内容如下:

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

如果我尝试访问http://example.com/file.html我收到以下错误:

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

它甚至可以加载DocumentRoot后面的文件吗?如果是这样,有人能指出我的方向吗?

回答

11

我相信你需要与

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

增加部分,将服务器配置Apache将能够从它提供什么之前。举个例子,我的DocumentRoot是/ var/WWW但本节中的默认可用站点:

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> 

然后,您可以重写URL去/ DOC /,服务器会知道从哪里得到的文件来自。

+0

很好的答案!谢谢 – 2009-02-27 13:49:17

0

只要你知道这是为什么规则不起作用:

的原因,它是不能够重写/home/other\_files/file.html是mod_rewrite并从mod_rewrite的的角度来看前面的斜杠解析路径/home/webroot/home/other\_files/file.html自等同于您的文档根目录/home/webroot

Ryan Ahearn's suggestion是一个体面的,可能是你想要去的路线。

0

这个功劳归功于Ryan Aheam,但我会拼出来。我是一名初学者,即使在Ryan的回答中,我也必须尝试几件事才能使语法正确。

我想让我的DocumentRoot成为我的cakephp目录。但后来我有一个Mantis Bug跟踪器,它只是普通的php,所以不在cakephp目录中。下面的文件我有以下工作。

http://www.my_website.com:通过在/ var/WWW/CakePHP的服务

http://www.my_website.com/mantisbt:通过服务的/ var/www/html等/ mantisbt

文件/etc/httpd/conf/httpd.conf中

Alias /mantisbt/ "/var/www/html/mantisbt/"                   
<Directory "/var/www/html/">                       
    AllowOverride All                        
</Directory>                           

<VirtualHost *:80>                         
    ServerAdmin [email protected]_email.com                    
    DocumentRoot /var/www/cakephp                     
    ServerName my_website.com                      
    <Directory /var/www/cakephp/>                     
     AllowOverride All                       
    </Directory>                          
</VirtualHost> 

文件/var/www/cakephp/.htaccess

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule ^mantisbt/?$ /mantisbt/ [NC,L] 
    RewriteRule ^$ webroot/ [L] 
    RewriteRule (.*) webroot/$1 [L] 
</IfModule>