php - mod_rewrite 和 mod_userdir 的问题

标签 php apache mod-rewrite apache2

我正在尝试获取其他人放在一起运行的网站的本地副本。该站点已部署并正常运行,我正在尝试在我的 public_html 目录下设置一个开发版本。

该站点使用 mod_rewrite 通过执行各种初始化步骤的网关脚本重定向所有流量。我有一个奇怪的问题,其中包含 .php 扩展名的链接有效,但那些没有扩展名的链接会死掉,但出现以下异常:

警告:未知:无法打开流:第 0 行未知中没有这样的文件或目录 fatal error :未知:需要打开失败'redirect:/~george/site/.fiona/core/gateway.php ' (include_path='.:/usr/share/php:/usr/share/pear') 在第 0 行未知

这是 .htaccess 文件的副本(针对我的本地安装进行了编辑)

RewriteEngine On
Options +FollowSymLinks

RewriteBase /~george/site/
RewriteCond %{REQUEST_URI} !.fiona/core/gateway.php
RewriteRule ^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$ .fiona/core/gateway.php?group=$1&destination=$2\.php [L,QSA]
RewriteCond %{REQUEST_URI} !.fiona/core/gateway.php
RewriteRule ^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$ .fiona/core/gateway.php?group=$1&destination=$2\.php [L,QSA]
RewriteCond %{REQUEST_URI} !.fiona/core/gateway.php
RewriteRule ^([a-zA-Z0-9_\-]+)(\.php)?$ .fiona/core/gateway.php?destination=$1\.php [L,QSA]

这是我在 mod_rewrite 日志中看到的内容

本地主机/~乔治/站点/登录

127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] applying pattern '^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (4) [perdir /home/george/public_html/site/] RewriteCond: input='/~george/site/login.php' pattern='!.fiona/core/gateway.php' => matched
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (2) [perdir /home/george/public_html/site/] rewrite 'login.php' -> '.fiona/core/gateway.php?destination=login.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) split uri=.fiona/core/gateway.php?destination=login.php -> uri=.fiona/core/gateway.php, args=destination=login.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] add per-dir prefix: .fiona/core/gateway.php -> /home/george/public_html/site/.fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (2) [perdir /home/george/public_html/site/] trying to replace prefix /home/george/public_html/site/ with /~george/site/
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (5) strip matching prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (4) add subst prefix: .fiona/core/gateway.php -> /~george/site/.fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (1) [perdir /home/george/public_html/site/] internal redirect with /~george/site/.fiona/core/gateway.php [INTERNAL REDIRECT]
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#21103668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'redirect:/~george/site/.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#21103668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'redirect:/~george/site/.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#21103668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'redirect:/~george/site/.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#21103668/initial] (1) [perdir /home/george/public_html/site/] pass through redirect:/~george/site/.fiona/core/gateway.php

本地主机/~george/site/login.php

127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (4) [perdir /home/george/public_html/site/] RewriteCond: input='/~george/site/login.php' pattern='!.fiona/core/gateway.php' => matched
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (2) [perdir /home/george/public_html/site/] rewrite 'login.php' -> '.fiona/core/gateway.php?destination=login.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) split uri=.fiona/core/gateway.php?destination=login.php -> uri=.fiona/core/gateway.php, args=destination=login.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] add per-dir prefix: .fiona/core/gateway.php -> /home/george/public_html/site/.fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (2) [perdir /home/george/public_html/site/] trying to replace prefix /home/george/public_html/site/ with /~george/site/
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (5) strip matching prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (4) add subst prefix: .fiona/core/gateway.php -> /~george/site/.fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (1) [perdir /home/george/public_html/site/] internal redirect with /~george/site/.fiona/core/gateway.php [INTERNAL REDIRECT]
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] applying pattern '^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri '.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (4) [perdir /home/george/public_html/site/] RewriteCond: input='/~george/site/.fiona/core/gateway.php' pattern='!.fiona/core/gateway.php' => not-matched
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri '.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-zA-Z0-9_\-]+)(\.php)?$' to uri '.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (1) [perdir /home/george/public_html/site/] pass through /home/george/public_html/site/.fiona/core/gateway.php

没有 .php 扩展名的链接似乎可以成功进行初始重定向,但是,由于某些原因,它们似乎无法重新路由到绝对路径。

我绝不是 mod_rewrite 方面的专家,所以这里很可能还有其他问题。任何想法都会很棒。

编辑 根据 Marius 的回答,我相信我已经将其缩小为 mod_userdir 与 mod_rewrite 结合的问题。将站点移动到/var/www 可以解决问题。我仍然想让它在我的 public_html 目录中工作。

编辑 Marius 让我更接近启用站点的解决方案。事实证明,问题是没有为用户目录启用 MultiViews(允许 apache 将索引解释为 index.php)。将以下内容添加到我启用的默认站点 (/etc/apache2/sites-enabled/000-default) 解决了 .php 扩展的问题,但禁用了 mod_rewrite。

<Directory /home/*/public_html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory> 

我启用的整个网站如下

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
    <Directory /home/*/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    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>

</VirtualHost>

最佳答案

我在使用“userdir”模块(使您能够使用 ~username 的模块)时看到了很多这样的问题。您可以在不使用 userdir 模块的“普通”目录中尝试完全相同的操作吗?

刚刚使用以下结构重新创建了您的网站:

marius@debiansrv:~/public_html/htaccesstest$ 查找 ./.菲奥娜 ./.菲奥娜/核心 ./.fiona/core/gateway.php ./.htaccess

这里的 htaccess 文件与您的 100% 相同,并且工作正常。你可以在这里查看: http://home.goodworkaround.com/~marius/htaccesstest/

您是否使用“.fiona”作为文件夹名称?我怀疑您可能正在使用“fiona”。此外,如果不是这种情况,您应该从 Apache 粘贴站点设置。

关于php - mod_rewrite 和 mod_userdir 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661747/

相关文章:

php - htaccess 301 重定向无法正常工作

php - 如何搜索 .module 文件的内容?

apache - 如何让apache访问日志显示主机头的值

apache - 使用 htaccess 阻止 URL

apache - 使用 mod_proxy_ajp 在端口 80 上运行 tomcat(错误)

apache - 如何在 beeline 中指定 TGT kerberos 票证缓存

sockets - 如何更改nginx proxy_pass中的request_uri?

php - 有条件的基于月份

php - 将具有相同名称的行中的表格单元格合并到php中的一个单元格或rowspan中

php - MySQL 查询行,3 个最高值?