XIMRX
XIMRX

Reputation: 2170

.htaccess showing 404 page for non existing urls but not for existing directories

My htaccess shows correct custom 404 page for non existing pages, but for directories without index file like domain.com/existingdirectory it shows default non custom internal error page. Cant figure out where is the issue.

ErrorDocument 400 /404.php
ErrorDocument 403 /404.php
ErrorDocument 408 /404.php
ErrorDocument 500 /404.php
ErrorDocument 404 /404.php

<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
</IfModule>

<Files .htaccess>
    Order Allow,Deny
    Deny from all
</Files>

Options All -Indexes
<IfModule mod_autoindex.c>
    Options -Indexes
</IfModule>

<IfModule mod_deflate.c>
    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>
    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE application/atom+xml \
                                      application/javascript \
                                      application/json \
                                      application/rss+xml \
                                      application/vnd.ms-fontobject \
                                      application/x-font-ttf \
                                      application/x-web-app-manifest+json \
                                      application/xhtml+xml \
                                      application/xml \
                                      font/opentype \
                                      image/svg+xml \
                                      image/x-icon \
                                      text/css \
                                      text/html \
                                      text/plain \
                                      text/x-component \
                                      text/xml
                                      application/ld+json \
                                      application/manifest+json \
                                      text/vtt \
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
    </IfModule>
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"
    ExpiresByType text/css                              "access plus 1 year"
    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"
    ExpiresByType image/x-icon                          "access plus 1 week"
    ExpiresByType text/x-component                      "access plus 1 month"
    ExpiresByType text/html                             "access plus 0 seconds"
    ExpiresByType application/javascript                "access plus 1 year"
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"
    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 month"
    ExpiresByType image/jpeg                            "access plus 1 month"
    ExpiresByType image/png                             "access plus 1 month"
    ExpiresByType video/mp4                             "access plus 1 month"
    ExpiresByType video/ogg                             "access plus 1 month"
    ExpiresByType video/webm                            "access plus 1 month"
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/font-woff2                "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType application/x-font-ttf                "access plus 1 month"
    ExpiresByType font/opentype                         "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"
    ExpiresByType image/jpg                             "access plus 1 month"
    ExpiresByType font/truetype                         "access plus 1 month"
    ExpiresByType text/javascript                       "access plus 1 year"

      <IfModule mod_headers.c>
        Header append Cache-Control "public"
      </IfModule>
</IfModule>

Upvotes: 0

Views: 204

Answers (1)

Cheery
Cheery

Reputation: 16214

Overwrite 403 message, too.

ErrorDocument 403 /404.php

Not sure why is is insider of mod_rewrite block and why do you want to enable rewrite engine there. You do not need the part below for custom error pages

<IfModule mod_rewrite.c> # remove this line 
    RewriteEngine On # remove
    RewriteBase / #remove
    ..... # keep the list of ErrorDocument, but remove everything else.
</IfModule>  # and also remove this line

As you can see from http://httpd.apache.org/docs/2.2/mod/core.html#errordocument - it is a core's feature, so it does not need any checkups for other modules. The final code instead of this block will look like

ErrorDocument 400 /404.php
ErrorDocument 403 /404.php
ErrorDocument 408 /404.php
ErrorDocument 500 /404.php
ErrorDocument 404 /404.php

Upvotes: 1

Related Questions