【问题标题】:Apache - .htaccess: mod_rewrite and ErrorDocument won't work togetherApache - .htaccess:mod_rewrite 和 ErrorDocument 不能一起工作
【发布时间】:2015-11-24 06:45:08
【问题描述】:

我不确定这里的错误是什么,但我无法将 404 错误重定向到特定页面。这是 .htaccess 的样子:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.+) index.php

ErrorDocument 404 http://www.someurl.com/error404/404.php

错误文件夹和文件存在,当我执行curl -i http://www.someurl.com/notexistent 时,它会返回正确的 404 错误代码。但没有重定向,既不卷曲,也不在浏览器中。这怎么可能? 抱歉,我不是这些 .htaccess 配置方面的专家,不胜感激。

编辑 1: 我尝试了不同的网址,例如:

ErrorDocument 404 http://www.someurl.com/error404/
ErrorDocument 404 /error404/404.php
ErrorDocument 404 /error404/

但没有成功。

编辑 2: 我忘了提:mod_rewrite 按预期工作。只有 404 重定向不起作用。

【问题讨论】:

    标签: apache .htaccess mod-rewrite http-status-code-404


    【解决方案1】:

    使用您的RewriteRule,您将所有没有真实文件或文件夹的页面重写为index.php,404 错误也不例外...

    尝试:

    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php? [L,R=404]
    

    【讨论】:

    • 谢谢你的回答:) 你能帮我解决这个问题吗?
    • 不,我不能如果我不知道你想做什么......你将所有没有真实文件的页面重写为index.php。不要那样做(删除RewriteRule ^(.+) index.php行)如果你喜欢404页面,只需使用ErrorDocument 404。在哪些情况下您希望看到此页面 404 以及在哪些情况下您希望看到index.php ???
    • 好的,对。我应该更具体一点。将所有不存在的页面重写为 index.php 的规则似乎不起作用。当我访问一个不存在的链接时,我只看到一个空白页面,尽管我没有重定向到 index.php 或 /。所以,我想做的是,当访问一个不存在的文件/文件夹时,a)应该重定向到 index.php 或/或 b)重定向到 404 页面。我更喜欢 a) 但是使用我的 .htaccess 这些重定向都不起作用:(
    • 现在尝试在答案部分使用我的 .htaccess。
    • 谢谢!我试过了,但没有成功:当我删除RewriteRule ^(.+) index.php 行时,将不存在的页面重定向到 index.php 确实有效,但页面的真实现有链接都不再有效。所以,我今天读了很多关于 php 的内容,然后查看了 index.php。我理解它的方式是,在 index.php 中有一个“查询”(尚不确定那到底是什么),我认为我需要修复/故障排除/编辑这个查询。所以我必须深入挖掘。我认为 .htaccess 没有问题 非常感谢您的帮助,并提出正确的问题 :)
    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2020-12-06
    • 2018-04-14
    • 2011-07-29
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多