为什么这个.htaccess文件将静态文件路由到我们的前端控制器?

时间:2017-08-21 18:32:45

标签: apache .htaccess mod-rewrite

我们多年来一直使用以下.htaccess文件。我们正在使用Apache 2.4.7。出于某种原因,静态文件似乎正在击中我们的前端控制器index.php

例如:我们的前端控制器正在处理https://example.com/apple-touch-icon.png,因为我们可以看到响应标头包含X-Powered-By:PHP/5.5.9-1ubuntu4.3

有人能发现这个问题吗?

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

1 个答案:

答案 0 :(得分:2)

您的ErrorDocument指令似乎导致此问题(在找不到某些资源,即图像,js或css文件时加载index.php

您可以注释掉此指令以避免此行为:

ErrorDocument 404 /index.php