Buttle Butkus
Buttle Butkus

Reputation: 9466

Apache mod_rewrite REQUEST_URI not matching pattern

Apache 2.4 on CentOS 7, on a cPanel/WHM remote server.

I want to get the Apache server's 404 response when a .jpg file is not found, instead of Rewriting to index.php which loads the entire web app.

Trying to do this in .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]

When I try to load these URLs that result in 404, example.com/asdf.jpg, or example.com/folder1/abc.css, the RewriteRule still goes into effect, they get rewritten to index.php, and the CMS 404 page gets loaded.

When I comment the RewriteRule, then those two urls result in the lightweight Apache 404 response instead of the CMS 404 response.

Complete mod_rewrite section of .htaccess:

<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>

Edit - here is the VirtualHost config:

<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>

Edit, I did rewrite trace6. It shows the .* rule being applied even though folder1 should not get the rule applied. I tried this on a virtual machine testing server and it works there, but not on this physical remote server.

Here are the first 13 lines of the trace:

[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/

Upvotes: 0

Views: 1604

Answers (1)

Walf
Walf

Reputation: 9318

It sounds like your rule is catching the subrequest when looking for the matching ErrorDocument. Try adding the NS flag to your rule.

Upvotes: 1

Related Questions