【问题标题】:Problems with mod_rewrite and mod_userdirmod_rewrite 和 mod_userdir 的问题
【发布时间】:2011-12-01 11:35:18
【问题描述】:

我正在尝试获取由其他人组合而成的网站的本地副本。该站点已部署且功能正常,我正在尝试在我的 public_html 目录下设置开发版本。

该站点使用 mod_rewrite 通过执行各种初始化步骤的网关脚本重定向所有流量。我有一个奇怪的问题,包括.php 扩展的链接可以工作,但是没有的链接会死掉,但有以下例外:

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0 Fatal error: Unknown: Failed opening required 'redirect:/~george/site/.fiona/core/gateway.php' (include_path='.:/usr/share/php:/usr/share/pear') in Unknown on line 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 日志中看到的内容

localhost/~george/站点/登录

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

localhost/~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 解释为 index.php)。将以下内容添加到启用的默认站点 (/etc/apache2/sites-enabled/000-default) 解决了 .php 扩展的问题,但禁用了 mod_rewrite。

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

我启用的整个网站如下

&ltVirtualHost *:80>
    ServerAdmin webmaster@localhost

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

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    &ltDirectory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    &lt/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/"
    &ltDirectory "/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
    &lt/Directory>

&lt/VirtualHost>

【问题讨论】:

    标签: php apache mod-rewrite apache2


    【解决方案1】:

    我在使用“userdir”模块(使您能够使用~username 的模块)时看到了很多这样的问题。你能用不使用 userdir 模块的“普通”目录尝试同样的事情吗?

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

    marius@debiansrv:~/public_html/htaccesstest$ 查找 ./.fiona ./.fiona/core ./.fiona/core/gateway.php ./.htaccess

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

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

    【讨论】:

    • 移动到 /var/www 绝对可以解决问题。为了更好地衡量,我还尝试移动到 /var/www/site,这也可以。该问题似乎与 mod_userdir 结合 mod_rewrite 有关。鉴于我需要为多个开发人员支持多个网站,如果可能的话,我真的很想使用 mod_userdir。
    • 刚刚编辑了我的帖子。你可以看看。
    • 路径实际上是.fiona。我会查看我的 Apache 设置并发布它们。
    • 启用的站点确实是问题所在。事实证明,我缺少 public_html 中的 MultiViews 并且 .php 是执行任何脚本所必需的。如果您要编辑答案以包含解决方案,我会接受。
    • 或许不是。 “AllowOverride None”禁用 mod_rewrite,但纠正了 .php 扩展名的问题。 “AllowOverride all”让 mod_rewrite 工作,但会破坏 MultiViews。
    【解决方案2】:

    原来问题出在我的站点启用文件上。显然用户目录没有正确配置以允许 mod_rewrite 工作。复制 /var/www/ 的 VirtualHost 设置并稍作修改即可解决此问题。

    &ltDirectory /home/*/public_html>
        Options Indexes FollowSymLinks
        AllowOverride all
        Order allow,deny
        allow from all
    &lt/Directory>

    【讨论】:

      【解决方案3】:

      它将查找您托管文件的基本目录相对于 URL 所在的位置。因此,如果您的站点实际上位于 http://example.com/~george/site/,那么您的 RewriteBase 将是正确的。之后,它将在该目录中查找某些内容,然后根据您的规则在 RewriteBase/.fiona/core/gateway.php 处查找它无法找到的内容。我相信如果您将 .fiona 目录放在 site/ 中,它应该可以工作。

      【讨论】:

      • 嗯.. 这似乎不是问题(使用 /~george/site/ 加载 index.php 没有问题)。我尝试了 ~george/site、/home/george/public_html/site 和 /home/george/site。都生成“内部服务器错误”
      • 对不起,我不知道为什么我没有考虑 RewriteBase 是相对于 URL 的。因此,您必须根据您的 public_html 文件夹将其定义为根文件夹。因此,如果您的实际站点位于example.com/site,那么您的 RewriteBase 应该是 /site/。编辑我的答案更合适
      • 如果我要直接导航到 gateway.php 文件,我必须输入什么地址?
      • 在浏览器中的 url 是 localhost/~george/peerscout/.fiona/core/gateway.php
      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      相关资源
      最近更新 更多