【问题标题】:Apache mod_rewrite REQUEST_URI not matching patternApache mod_rewrite REQUEST_URI 不匹配模式
【发布时间】:2017-03-23 23:04:38
【问题描述】:

CentOS 7 上的 Apache 2.4,在 cPanel/WHM 远程服务器上。

我想在找不到 .jpg 文件时获得 Apache 服务器的 404 响应,而不是重写到加载整个 Web 应用程序的 index.php。

尝试在.htaccess 中执行此操作。

#Trying to exclude jpegs:
RewriteCond %{REQUEST_URI} !\.jpg$
#And exclude these folders:
RewriteCond %{REQUEST_URI} !^/(folder1|folder2)/
#And rewrite everything else (commenting this results in regular Apache 404 responses for missing image files):
RewriteRule .* index.php [L]

当我尝试加载这些导致404example.com/asdf.jpgexample.com/folder1/abc.css 的URL 时,RewriteRule 仍然有效,它们被重写为index.php,CMS 页面404被加载。

当我评论 RewriteRule 时,这两个 URL 会生成轻量级 Apache 404 响应,而不是 CMS 404 响应。

完成.htaccessmod_rewrite部分:

<IfModule mod_rewrite.c>
############################################
## enable rewrites

    Options +FollowSymLinks
    RewriteEngine on

        RewriteBase /


    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(folder1|folder2)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
## test excluding .jpg from rewrite
    RewriteCond %{REQUEST_URI} !(\.jpg)$

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

</IfModule>

编辑 - 这是VirtualHost 配置:

<VirtualHost 12.34.56.7:80>
  ServerName site.example.com
  ServerAlias www.site.example.com
  DocumentRoot /home/someuser/public_html
  ServerAdmin webmaster@site.example.com
  UseCanonicalName Off
  CustomLog /etc/apache2/logs/domlogs/site.example.com combined
  <IfModule log_config_module>
    <IfModule logio_module>
      CustomLog /etc/apache2/logs/domlogs/site.example.com-bytes_log "%{%
s}t %I .\n%{%s}t %O ."
    </IfModule>
  </IfModule>
  ## User someuser # Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        UserDir enabled someuser
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache
 versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacy
ExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule include_module>
    <Directory "/home/someuser/public_html">
      SSILegacyExprParser On
    </Directory>
  </IfModule>

  <IfModule suphp_module>
    suPHP_UserGroup someuser someuser
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup someuser someuser
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid someuser someuser
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID someuser someuser
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/someuser/public_html/cgi-bin/
  </IfModule>

  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/std/2_4/someuser/csite.example.com/*.conf"
</VirtualHost>

编辑,我确实重写了trace6。它显示正在应用 .* 规则,即使 folder1 不应应用该规则。我在虚拟机测试服务器上试过这个,它在那里工作,但在这个物理远程服务器上不行。

这是跟踪的前 13 行:

[Fri Nov 11 02:21:08.389721 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] [perdir /home/someuser/public_html/] add path info postfix: /home/someuser/public_html/folder1/catalog/ABC -> /home/someuser/public_html/folder1/catalog/ABC/10162015/Asdf.jpg, referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389764 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] [perdir /home/someuser/public_html/] strip per-dir prefix: /home/someuser/public_html/folder1/catalog/ABC/10162015/Asdf.jpg -> folder1/catalog/ABC/10162015/Asdf.jpg, referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389775 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] [perdir /home/someuser/public_html/] applying pattern '.*' to uri 'folder1/catalog/ABC/10162015/Asdf.jpg', referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389790 2016] [rewrite:trace5] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] setting env variable'HTTP_AUTHORIZATION' to '', referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389802 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] [perdir /home/someuser/public_html/] add path info postfix: /home/someuser/public_html/folder1/catalog/ABC -> /home/someuser/public_html/folder1/catalog/ABC/10162015/Asdf.jpg, referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389812 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] [perdir /home/someuser/public_html/] strip per-dir prefix: /home/someuser/public_html/folder1/catalog/ABC/10162015/Asdf.jpg -> folder1/catalog/ABC/10162015/Asdf.jpg, referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389821 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] [perdir /home/someuser/public_html/] applying pattern '.*' to uri 'folder1/catalog/ABC/10162015/Asdf.jpg', referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389835 2016] [rewrite:trace4] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] [perdir /home/someuser/public_html/] RewriteCond: input='/folder1/catalog/ABC/10162015/Asdf.jpg' pattern='!^/(folder1|folder2)/' => not-matched, referer:http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389844 2016] [rewrite:trace1] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f002b310/initial] [perdir /home/someuser/public_html/] pass through /home/someuser/public_html/folder1/catalog/ABC, referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389922 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f001bae8/initial/redir#1] [perdir /home/someuser/public_html/] strip per-dir prefix: /home/someuser/public_html/404.shtml -> 404.shtml, referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389945 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f001bae8/initial/redir#1] [perdir /home/someuser/public_html/] applying pattern '.*' to uri '404.shtml', referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389958 2016] [rewrite:trace5] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f001bae8/initial/redir#1] setting env variable 'HTTP_AUTHORIZATION' to '', referer: http://site.example.com/widgets/bathroom/
[Fri Nov 11 02:21:08.389969 2016] [rewrite:trace3] [pid 25637:tid 139651102975744] mod_rewrite.c(477): [client 45.67.87.76:57196] 45.67.87.76 - - [site.example.com/sid#7f031c7731e0][rid#7f02f001bae8/initial/redir#1] [perdir /home/someuser/public_html/] strip per-dir prefix: /home/someuser/public_html/404.shtml -> 404.shtml, referer: http://site.example.com/widgets/bathroom/

【问题讨论】:

  • 在这些之上/之下还有其他规则吗?
  • 整件事都包含在if module 中,用于mod_rewrite,然后我的问题中指令之前的唯一内容与 http/https 相关,这似乎不会影响这个问题.之后就什么都没有了。
  • 您的规则看起来不错,我什至尝试在我的服务器上重现该问题,但一切正常。检查类型、浏览器缓存并启用重写跟踪日志记录。
  • @anubhava 但以前的RewriteCond 不应该阻止.* 应用于我要排除的文件吗?另外,您的重写规则不是完全没用吗,因为它只是将index.php 重写为自己? ewriteRule !^index\.php$ index.php [L]????
  • @anubhava 很抱歉我错过了!,但它仍然不能解决问题。 .jpg 仍在重写为 index.php,触发 CMS 响应。另外,.*!^index\.php$ 之间真的有什么区别吗?不多,对吧?既然重点是重写.jpg(和其他一些)文件,那么正则表达式真的没有区别,对吧?

标签: apache .htaccess mod-rewrite cpanel


【解决方案1】:

听起来您的规则在查找匹配的ErrorDocument 时捕获了子请求。尝试将 NS 标志添加到您的规则中。

【讨论】:

  • 我把规则改成这个了,结果还是和以前一样。 RewriteRule .* index.php [L,NS]
  • 如果您没有设置ErrorDocuments,Apache 可以在它们上获得内部 404,因此您的规则可能会以某种方式捕捉到它。你可以访问服务器配置,所以use loggingtrace8 看看发生了什么。
  • 我发布了 115 行的前 13 行 trace6。你能告诉我什么吗?还是我应该发布更多(或全部)?
  • .jpg 与该 URL 无关,它以 /folder1/ 开头,将其排除在 RewriteRule 之外,所以这就是它试图去的地方。当然.* 匹配,它会匹配任何东西,但这并不意味着规则已应用RewriteConds 会阻止它。看起来您在 folder1 的某个地方有一些东西正在做更多的重写或别名,可能带有 PT 标志,如该日志部分的第 8 行和第 9 行所示。
  • 您认为/folder1/ 内部可能存在.htaccess 的想法很好,但我查了一下,没有。但这让我开始思考,我在 apache 的配置文件夹中做了一个grep -lr "404" .。我找到了一个名为errordocument.conf 的文件,其中包含ErrorDocument 404 /404.shtml 这一行。因此,当 Apache 无法找到 .jpg 文件时,它会生成 404 并立即在我的站点根目录中查找 404.shtml。我没有,所以现在(我猜)会产生另一个 404?由于/404.shtml 不符合我的任何规则,它被重写为index.php
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2011-09-29
  • 2019-03-14
  • 2011-08-06
  • 2015-04-28
  • 2012-01-20
相关资源
最近更新 更多