Welcome Guest, Not a member yet? Register   Sign In
apache mod_rewrite problem: all css/js/images 404
#1

Rather than using  an .htaccess file, I'm trying to get the basic CodeIgniter mod_rewrite files working in my apache conf file. I'm doing this because apache says .htaccess files cause a performance hit.
Quote:You should avoid using .htaccess files completely if you have access to httpd main server config file.

The mod_rewrite section of my apache conf, which is based on the default htaccess file, looks like this:
Code:
        <IfModule mod_rewrite.c>
                RewriteEngine On

                # If you installed CodeIgniter in a subfolder, you will need to
                # change the following line to match the subfolder you need.
                # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
                #RewriteBase /

                # Redirect Trailing Slashes...
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule ^(.*)/$ $1 [L,R=301,END]

                # Rewrite "example.com" => "www.example.com", leave all other subdomains alone
                RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
                RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R=301,QSA,END]

                # Checks to see if the user is attempting to access a valid file,
                # such as an image or css document, if this isn't true it sends the
                # request to the front controller, index.php
                # JTA NOTE this is different than default .htaccess file from CI4
                # because it's in the apache conf instead -- THE SLASH BEFORE
                # index.php IS CRITICAL
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule ^(.*)$ /index.php$1 [L,NC,QSA]

        </IfModule>

Note that the www redirect is a bit different in my case due to different requirements.

This mostly works, but for some reason it also rewrites all of my CSS/JS/images also despite the !-f and !-d rewrite conditions. Can anyone tell me how to modify this so that it prevents rewriting of actual files to the index.php file?
Reply
#2

Debug mod_rewrite.
https://stackoverflow.com/questions/9632...od-rewrite
Reply
#3

(02-12-2021, 07:02 PM)kenjis Wrote: Debug mod_rewrite.
https://stackoverflow.com/questions/9632...od-rewrite
Sadly, the debug information wasn't very informative. I amped up rewrite reporting to its maximum level, trace8, but the most telling lines don't tell you much:
Code:
[Fri Feb 12 12:42:22.201839 2021] [rewrite:trace2] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] init rewrite engine with requested uri /js/bootstrap.js
[Fri Feb 12 12:42:22.201851 2021] [rewrite:trace3] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] applying pattern '^(.*)/$' to uri '/js/bootstrap.js'
[Fri Feb 12 12:42:22.201860 2021] [rewrite:trace3] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] applying pattern '^(.*)$' to uri '/js/bootstrap.js'
[Fri Feb 12 12:42:22.201870 2021] [rewrite:trace4] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] RewriteCond: input='www.example.com' pattern='^example\\.com$' [NC] => not-matched
[Fri Feb 12 12:42:22.201876 2021] [rewrite:trace3] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] applying pattern '^(.*)$' to uri '/js/bootstrap.js'
[Fri Feb 12 12:42:22.201887 2021] [rewrite:trace4] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] RewriteCond: input='/js/bootstrap.js' pattern='!-f' => matched
[Fri Feb 12 12:42:22.201895 2021] [rewrite:trace4] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] RewriteCond: input='/js/bootstrap.js' pattern='!-d' => matched
[Fri Feb 12 12:42:22.201901 2021] [rewrite:trace2] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] rewrite '/js/bootstrap.js' -> '/index.php/js/bootstrap.js'
[Fri Feb 12 12:42:22.201907 2021] [rewrite:trace2] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] local path result: /index.php/js/bootstrap.js
[Fri Feb 12 12:42:22.201924 2021] [rewrite:trace2] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] prefixed with document_root to /var/www/example/public/index.php/js/bootstrap.js
[Fri Feb 12 12:42:22.201933 2021] [rewrite:trace1] [pid 48329:tid 140340508313344] mod_rewrite.c(483): [client 127.0.0.1:35566] 127.0.0.1 - - [www.example.com/sid#7fa393ce1ab0][rid#7fa39024f0a0/initial] go-ahead with /var/www/example/public/index.php/js/bootstrap.js [OK]

I looked into the documentation and saw a curious (but also somewhat cryptic) bit information about REQUEST_FILENAME:

Quote:The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI. Depending on the value of AcceptPathInfo, the server may have only used some leading components of the REQUEST_URI to map the request to a file.

Evidently, because I've got this rewrite stuff in the apache conf in a <VirtualHost> block, the server doesn't yet know exactly what the local file system path is. I visited the #apache IRC chat and a kind person there suggested putting %{DOCUMENT_ROOT} ahead of the %{REQUEST_FILENAME} for those vital lines and this has fixed the problem:
Code:
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php$1 [L,NC,QSA]

This person on the apache IRC chat suggested eliminating mod_rewrite entirely and using FallbackResource instead.
Code:
FallbackResource /index.php
Reply
#4

What does it tell you when you use the we browsers developer tools F12 ?

It could be a CORS problem.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(02-12-2021, 09:59 PM)InsiteFX Wrote: What does it tell you when you use the we browsers developer tools F12 ?

It could be a CORS problem.
the domain requests are all consistent. Every asset reference in the HTML is a relative reference. Attempting to access an asset directly was leading to 404. I.e., I'd just type this in the browser url bar and get 404: https://www.example.com/js/bootstrap.js

To reiterate, the problem stems from the fact that I was putting my mod_rewrite rules in the apache conf rather than in the .htaccess file. In an .htaccess file, Apache knows what the local file system is. Apparently when these rules are evaluated in the apache conf, the local file system location has not yet been worked out by Apache so you must prepend %{DOCUMENT_ROOT} to %{REQUEST_FILENAME} for the main RewriteRule.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB