【发布时间】: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]
当我尝试加载这些导致404、example.com/asdf.jpg 或example.com/folder1/abc.css 的URL 时,RewriteRule 仍然有效,它们被重写为index.php,CMS 页面404被加载。
当我评论 RewriteRule 时,这两个 URL 会生成轻量级 Apache 404 响应,而不是 CMS 404 响应。
完成.htaccess的mod_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