【问题标题】:ErrorDocument 404 not working with routingErrorDocument 404 不适用于路由
【发布时间】:2014-02-02 16:34:37
【问题描述】:

我尝试在 .htacces 上使用此代码在我的页面上拥有我自己的自定义 404。

ErrorDocument 404 http://mysite.com/404.shtml

当我访问网站上不存在的页面时。它必须显示类似这样的内容

但这是它显示的那个

这是我完整的htaccess

<files .htaccess>
    order allow,deny
    deny from all
</files>
php_value memory_limit 170M

RewriteEngine on


Options +FollowSymlinks
Options -Indexes



RewriteCond %{REQUEST_URI} (.+)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^www.mysite.com.*$ [NC]
RewriteRule ^(.*)$ http://mysite.com%{REQUEST_URI} [L,R=301]  

ErrorDocument 404 http://mysite.com/404.shtml

我的htaccess的位置与网站的404.shtmlindex.php一起定位

删除此代码将解决问题。我怎样才能使它与路由一起工作。

   RewriteCond %{REQUEST_URI} (.+)$  [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    AddDefaultCharset utf-8
    RewriteRule (.*) index.php

【问题讨论】:

  • 在“它必须显示类似这样的东西”之后缺少一些东西
  • 你在 Apache 下运行吗?您应该检查.htaccess 是否启用:enable htaccess on apache
  • 是的。 .htaccess 已启用
  • 我不知道其他线路如何影响ErrorDocument 线路。但是这个看起来不错。尝试评论其他东西或将.htaccessErrorDocument开始逐步改写成。
  • 删除此代码将使其正常工作。 RewriteCond %{REQUEST_URI} (.+)$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d AddDefaultCharset utf-8 RewriteRule (.*) index.php

标签: .htaccess http-status-code-404 custom-error-pages


【解决方案1】:

不要使用 ErrorDocument 404 http://mysite.com/404.shtml 引用整个 url,而是将文档命名为相对于您的 Web 根目录。在这种情况下,/404.shtml

【讨论】:

【解决方案2】:

在您的 .htaccess 中使用这些代码来捕获 404 错误页面。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.html [L]
</IfModule>

现在您的所有 404 请求都将转到 /404.html。您还可以使用 /404.php 进行更动态的编码。还要确保在您的 apache 中启用了重写模块。

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 2015-10-20
    • 1970-01-01
    • 2018-01-04
    • 2018-12-31
    • 2016-12-07
    相关资源
    最近更新 更多