.htaccess显示非现有网址的404页面,但不显示现有目录

时间:2014-10-12 20:53:43

标签: apache .htaccess

我的htaccess显示非现有页面的正确自定义404页面,但对于没有索引文件的目录,如 domain.com/existingdirectory ,它会显示默认的非自定义内部错误页面。无法弄清问题在哪里。

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>

1 个答案:

答案 0 :(得分:1)

也覆盖403消息。

ErrorDocument 403 /404.php

不确定为什么是mod_rewrite块的内部人员,为什么要在那里启用重写引擎?您不需要以下部分来获取自定义错误页面

<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

http://httpd.apache.org/docs/2.2/mod/core.html#errordocument可以看出 - 它是核心功能,因此不需要对其他模块进行任何检查。最终代码而不是此块看起来像

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