Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自定义404错误页面--仅适用于PHP页面_Php_Apache_.htaccess_Http Status Code 404 - Fatal编程技术网

自定义404错误页面--仅适用于PHP页面

自定义404错误页面--仅适用于PHP页面,php,apache,.htaccess,http-status-code-404,Php,Apache,.htaccess,Http Status Code 404,我有一个带有htaccess的404个性化页面 ErrorDocument 404 /404.php 它工作得很好,但需要一切。jpg、.js、.css等… 我希望它只取.php并为所有其他代码执行正常的404错误 我在任何地方都找不到这个选择 非常感谢。有几种可能的解决方法,但我会执行以下操作之一: 您可以使用@LazyOne建议的mod_rewrite,仅将扩展名为.php的文件重定向到自定义文档: RewriteEngine on RewriteCond %{REQUEST_FILENA

我有一个带有htaccess的404个性化页面

ErrorDocument 404 /404.php
它工作得很好,但需要一切。jpg、.js、.css等…
我希望它只取.php并为所有其他代码执行正常的404错误

我在任何地方都找不到这个选择


非常感谢。

有几种可能的解决方法,但我会执行以下操作之一:

您可以使用@LazyOne建议的
mod_rewrite
,仅将扩展名为
.php
的文件重定向到自定义文档:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f # Only redirect to error for files that don't exist
RewriteRule ^(.*)\.php$ /404.php [L]
或者,您也可以像以前一样通过PHP错误处理程序处理所有内容,并使用PHP确定请求的资源是否为PHP脚本:

<?php

  if (strtolower(substr($_SERVER['SCRIPT_URL'], -4)) != '.php') {
    // If the requested resource does not have a .php extension, include the standard error doc and exit
    include '404.html';
    exit;
  }

  // Handle .php 404s here

不确定,但尝试一下:

<Files ~ "\.php$">
    ErrorDocument 404 /404.php
</Files>

ErrorDocument 404/404.php
引用文件:

该指令限制了所附文件的范围 按文件名显示指令

利用,您可以轻松地检查导致错误的文件的结尾,如果它不符合您的需要或只是呈现不同的404,则将头抛出到正常的404

把这个放到404.php文件中

$bad_url = $_SERVER['REQUEST_URI'];
$bad_extension = substr(strrchr($bad_url,'.'),1);

if($bad_extension != "php"){
    //Not PHP
}else{
    //PHP
}

你所说的“它需要一切”是什么意思?404是未找到的任何HTTP请求的响应代码,无论是php请求还是图像。我的意思是,如果我执行www.example.com/img/errorimg.jpg,他将引导我访问www.example.com/404.php,我不希望这样……您可以尝试通过URL重写(mod_rewrite)来实现这一点--将对任何不存在的.php文件的请求重定向到您的自定义404.php--这是可能的(仅3行或4行)为什么您不希望这样?@您的常识我希望这样,例如,我在调用图像、js或其他任何东西时犯了错误。。。在控制台中,它将执行一个错误
工作正常!非常感谢您的帮助:-)