.htaccess Mod Rewrite在DocumentRoot后面加载文件

.htaccess Mod Rewrite在DocumentRoot后面加载文件,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我正在使用.htaccess和mod\u rewrite指向位于DocumentRoot后面的文件。我的文件夹结构如下所示: home/ webroot/ other_files/ 我在webroot中有一个.htaccess文件,其中包含以下内容: RewriteEngine on RewriteRule ^(.*)$ /home/other_files/$1 重新启动发动机 重写规则^(.*)$/home/other_文件/$1 如果我尝试访问,我会收到以下错误: 在此服务

我正在使用
.htaccess
mod\u rewrite
指向位于DocumentRoot后面的文件。我的文件夹结构如下所示:

home/
    webroot/
    other_files/
我在webroot中有一个
.htaccess
文件,其中包含以下内容:

RewriteEngine on RewriteRule ^(.*)$ /home/other_files/$1 重新启动发动机 重写规则^(.*)$/home/other_文件/$1 如果我尝试访问,我会收到以下错误:

在此服务器上找不到请求的URL/home/other_files/file.html


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

我认为您需要添加一个带有

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

(选项)
在apache能够从中提供任何服务之前,请先访问您的服务器配置。例如,my 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>
Alias/doc/“/usr/share/doc/”
选项索引多视图跟随符号链接
不允许超限
命令拒绝,允许
全盘否定
允许从127.0.0.0/255.0.0.0::1/128开始

然后,您可以重写URL以转到/doc/,服务器将知道从何处获取文件。

这样您就可以知道该规则不起作用的原因:


它无法重写为
/home/other_files/file.html
的原因是mod_rewrite将路径解析为
/home/webroot/home/other_files/file.html
,因为从mod_rewrite的角度来看,前面的斜杠相当于
/home/webroot
的文档根


这是一个不错的选择,很可能是你想要走的路线。

这归功于Ryan Aheam,但我会详细说明。我是一个初学者,即使是Ryan的答案,我也不得不尝试一些东西来获得正确的语法

我希望我的
DocumentRoot
成为我的
cakephp
目录。但是我有一个螳螂Bug跟踪器,它只是普通的PHP,所以不在
cakephp
目录中。下面的文件我有以下工作

http://www.example.com
:由
/var/www/cakephp

http://www.example.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 me@my_email.com                                                                             
    DocumentRoot /var/www/cakephp                                                                                   
    ServerName my_website.com                                                                                      
    <Directory /var/www/cakephp/>                                                                                   
        AllowOverride All                                                                                           
    </Directory>                                                                                                    
</VirtualHost>

我尝试在没有分区的情况下将Alias访问到DocumentRoot之外的目录,结果成功了…@Pere您需要在某个地方授予对“DocumentRoot之外的目录”的访问权限。如果您不必为该目录显式地包含
部分,那么您可能已经在配置中的其他位置包含了授予对父目录访问权限的
部分。“mod_rewrite正在将路径解析为
/home/webroot/home/other_files/file.html
”-是。除非该规则是在服务器或virtualhost上下文中定义的(即not
.htaccess
),否则在这种情况下,它也将接受绝对文件系统路径(以及根相对URl路径)。“我将详细说明它”-尽管您还需要授予对区域的访问权限(在文档根之外)这是
别名的目标(正如Ryan在回答中所做的,尽管是针对Apache2.2)。您可能已经对父目录执行了此操作,不过最好只对有问题的目录执行此操作。此外,除非从多个vHost访问
/mantisbt
,否则最好在相关vHost配置中包含这些指令,而不是在服务器配置中包含“全局”指令。
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^mantisbt/?$   /mantisbt/  [NC,L]
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>