【问题标题】:Apache vhost: Alias and RewriteRuleApache vhost:别名和 RewriteRule
【发布时间】:2018-10-06 07:40:40
【问题描述】:

我有一个 URL(域),用于多个目录(项目)。将 .htaccess 放入其中时,我有一个非常奇怪的行为。

  • sub.mywebsite.com/ => /foo/bar/things
  • sub/mywesite.com/pro => /srv/www/new

虚拟主机:

<VirtualHost *:80>
    DocumentRoot /foo/bar/things
    ServerName sub.mywebsite.com
    Alias /pro /srv/www/new
</VirtualHost>

而且效果很好。当我继续sub.mywebsite.com/ 时,它会点击/foo/bar/things 目录,反之亦然。


现在,让我们在/srv/www/new 中添加一个简单的.htaccess

RewriteEngine on
RewriteRule ^(.*)/$    php/file.php?text=$1   [L,QSA]

所以当我继续 sub.mywebsite.com/pro/hello/ 时,调用的文件应该是带有 GET 参数的 /srv/www/new/php/file.php。我仔细检查了一下,规则很好。

RewriteLogLevel 4 到此结束:

192.168.123.123 - - [25/Apr/2018:19:28:11 +0200] [sub.mywebsite.com/sid#7f317dd08b8][rid#7f37eca9f78/initial] (1) 
[perdir /srv/www/new/] internal redirect with /srv/www/new/php/file.php [INTERNAL REDIRECT]

然后online htaccess checker 确认:The new url is http://sub.mywebsite.com/php/file.php%3Ftext=testing

但我找不到 apache 404:

Not Found
The requested URL /srv/www/new/php/file.php was not found on this server.

是的,ls -l /srv/www/new/php/file.php 文件存在。

非常奇怪的部分是这样的:在 apache 错误日志中,我有:

[Wed Apr 25 19:45:30 2018] [error] [client 192.168.123.123] File does not exist: /foo/bar/things/srv

问题

  1. 那是什么鬼?为什么error_log中的文件不一样 作为我的浏览器上显示的那个?
  2. 为什么 apache 无法加载正确的文件 /srv/www/new/php/file.php 作为 .htaccess 的要求?

如果需要,请随时向我询问更多数据。我想明白!

注意:/foo/bar/things 中没有 .htaccess ;我在我的虚拟主机中尝试了很多东西,比如添加&lt;directory&gt; 等。

【问题讨论】:

  • 您很可能需要将 RewriteBase "/pro" 添加到您的 .htaccess 中
  • @DusanBajic 该死的你实际上是对的。你能否在答案中详细说明,也许尝试解释这两个问题,这样我才能接受你的贡献?非常感谢!
  • 我可能没有足够的关于 httpd 内部的知识来给出正确的解释,但我会尝试。

标签: apache .htaccess unix alias virtualhost


【解决方案1】:

Apache > 2.4.16 更新

从版本 2.4.16 开始,当请求通过 Alias 映射时,不需要 RewriteBase 指令:

当您在每个目录 (htaccess) 上下文中使用相对路径进行替换时,此指令是必需的,除非满足以下任何条件:

  • 原始请求和替换位于 [DocumentRoot](https://httpd.apache.org/docs/current/mod/core.html#documentroot) 下方(而不是通过其他方式访问,例如 [Alias](https://httpd.apache.org/docs/current/mod/mod_alias.html#alias))。
  • 包含[RewriteRule](https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule) 的目录的文件系统 路径(以相对替换为后缀)也可以作为服务器上的 URL 路径(这种情况很少见)。
  • 在 Apache HTTP Server 2.4.16 及更高版本中,当请求通过 [Alias](https://httpd.apache.org/docs/current/mod/mod_alias.html#alias)[mod_userdir](https://httpd.apache.org/docs/current/mod/mod_userdir.html) 映射时,可以省略此指令。

所以基本上你不会再收到任何AH00128: File does not exist: 错误了。

值得注意的是,CentOS 7.x 随附带有安全补丁的 Apache 2.4.6:至于 2022/01/07,您将获得修复 CVE-2021-40438 的 2.4.6-97.2。因此,如果您想管理多个别名,则需要使用不同的软件包进行升级,但这是另一回事...

【讨论】:

    【解决方案2】:

    您的两个问题的答案已经显示在您的屏幕和日志中:

    The requested URL /srv/www/new/php/file.php was not found on this server.
    

    请求的URL

    AH00128: File does not exist: /foo/bar/things/srv/www/new/php/file.php
    

    文件不存在。

    因此,apache 不会尝试获取位于文件系统上 /srv/www/new/php/ 文件夹中的文件 file.php。它正在尝试打开 URL http://sub.mywebsite.com/srv/www/new/php/file.php,它解释了浏览器错误和错误日志中的行,因为对于该 URL,它确实应该查看 /foo/bar/things/srv/www/new/php/file.php

    来自http://httpd.apache.org/docs/current/mod/mod_rewrite.html

    重写引擎可用于 .htaccess 文件和 部分,具有一些额外的复杂性。
    (...)
    查看 RewriteBase 指令以获取有关前缀的更多信息 将被添加回相对替换。
    (...)
    RewriteBase 指令指定要用于的 URL 前缀 每个目录 (htaccess) 的 RewriteRule 指令替换 相对路径。当您使用亲戚时,此指令是必需的 每个目录 (htaccess) 上下文中的替换路径。
    (...)
    这种错误配置通常会导致服务器查找 文档根目录下的目录。

    解决方案:将RewriteBase "/pro" 添加到您的.htaccess

    可以在Rewrite Log中看到区别:

    没有 RewriteBase:

    [perdir /srv/www/new/] rewrite 'hello/' -> 'php/file.php?text=hello'
    [perdir /srv/www/new/] add per-dir prefix: php/file.php -> /srv/www/new/php/file.php
    [perdir /srv/www/new/] internal redirect with /srv/www/new/php/file.php [INTERNAL REDIRECT]
    

    RewriteBase "/pro":

    [perdir /srv/www/new/] rewrite 'hello/' -> 'php/file.php?text=hello'
    [perdir /srv/www/new/] add per-dir prefix: php/file.php -> /srv/www/new/php/file.php
    [perdir /srv/www/new/] trying to replace prefix /srv/www/new/ with /pro
    strip matching prefix: /srv/www/new/php/file.php -> php/file.php
    add subst prefix: php/file.php -> /pro/php/file.php
    [perdir /srv/www/new/] internal redirect with /pro/php/file.php [INTERNAL REDIRECT]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-01
      • 2011-02-07
      • 2013-05-07
      • 2014-10-18
      • 2016-01-16
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多