2016-11-10 96 views
0

CentOS 7上的Apache 2.4,在cPanel/WHM遠程服務器上。Apache mod_rewrite REQUEST_URI不匹配模式

我想在沒有找到.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] 

當我嘗試加載這些URL導致404example.com/asdf.jpg,或example.com/folder1/abc.cssRewriteRule仍然生效,他們得到改寫爲index.php和CMS 404頁面被加載。

當我評論RewriteRule時,那麼這兩個網址會產生輕量級的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 [email protected] 
    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/ 
+0

上面/下面是否還有其他規則? –

+0

整個事情被包含在'mod_rewrite'的'if module'中,然後我的問題中的指令之前唯一與http/https相關的東西似乎不會影響這個問題。之後沒有什麼。 –

+0

你的規則看起來不錯,我甚至試圖在我的服務器上重現這個問題,但它的工作方式和你所期望的一樣。檢查類型,瀏覽器緩存並啓用重寫跟蹤日誌記錄。 –

回答

1

這聽起來像尋找匹配ErrorDocument當您的規則正在迎頭趕上子請求。嘗試將NS標誌添加到您的規則。

+0

我改變了這個規則,但結果和以前一樣。 'RewriteRule。* index.php [L,NS]' –

+0

如果你沒有設置'ErrorDocument',Apache可以在它們上面獲得一個內部的404,所以它可能是你的規則以某種方式捕獲的。您可以訪問服務器配置,因此[trace8]使用[使用日誌記錄](http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#logging)來查看發生了什麼。 – Walf

+0

我發佈了115行'trace6'的前13行。你可以告訴任何事情嗎?或者我應該發佈更多(或全部)? –