Abs
Abs

Reputation: 57946

Why is this .htaccess file routing static files to our front controller?

We have been using the following .htaccess file for years. We are using Apache 2.4.7. And for some reason, static files seem to be hitting our front controller index.php!

For example: https://example.com/apple-touch-icon.png is being handled by our front controller as we can see the response header includes X-Powered-By:PHP/5.5.9-1ubuntu4.3.

Can anyone spot the issue?

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Force SSL
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    ### Canonicalize codeigniter URLs

    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]

    # Enforce NO www
    RewriteCond %{HTTP_HOST} ^www [NC]
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    # 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 index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

Upvotes: 1

Views: 110

Answers (1)

anubhava
anubhava

Reputation: 785541

It appears that your ErrorDocument directive is causing this (loading index.php when some resource i.e. image, js or css file is not found.

You can comment out this directive to avoid this behavior:

ErrorDocument 404 /index.php

Upvotes: 2

Related Questions