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

时间:2022-10-09 18:53:35

I have a personalized page for 404 with htaccess

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

ErrorDocument 404 /404.php

It works fine, but it takes everything .jpg, .js, .css etc...
I would like it to just take the .php and do a normal 404 error for all others.

它工作正常,但它需要所有.jpg,.js,.css等...我希望它只是采取.php并为所有其他人做一个正常的404错误。

I didn't find that option anywhere.

我没有在任何地方找到那个选项。

Thank you very much.

非常感谢你。

3 个解决方案

#1


5  

Not sure but give this a try:

不确定,但尝试一下:

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

Quote from Files Directive documentation:

从文件指令文档引用:

The <Files> directive limits the scope of the enclosed directives by filename.

指令通过filename限制所附指令的范围。

#2


2  

There are a few possible approaches to this, but I would do one of the following:

有一些可能的方法,但我会做以下之一:

You can use mod_rewrite, as suggested by @LazyOne, to redirect only files with a .php extension to your custom document:

您可以使用@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]

Or you can direct everything through the PHP error handler as you already do, and use PHP to determine whether the requested resource is a PHP script or not:

或者您可以像通常那样通过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

The disadvantage of this second approach is that you may need to rewrite the standard error document so that it used PHP to get dynamic information to display to the user, such and the requested resource path.

第二种方法的缺点是您可能需要重写标准错误文档,以便它使用PHP来获取动态信息以显示给用户,例如所请求的资源路径。

#3


1  

Utilizing the $_SERVER['REQUEST_URI'], you can easily check the ending of the file that resulted in an error and then throw a header to a normal 404 if it doesn't correspond to your needs or just render a different 404

使用$ _SERVER ['REQUEST_URI'],您可以轻松检查导致错误的文件的结尾,然后如果它不符合您的需要或仅渲染不同的404,则将标头抛出到正常404

Put this in your 404.php file

把它放在你的404.php文件中

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

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

#1


5  

Not sure but give this a try:

不确定,但尝试一下:

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

Quote from Files Directive documentation:

从文件指令文档引用:

The <Files> directive limits the scope of the enclosed directives by filename.

指令通过filename限制所附指令的范围。

#2


2  

There are a few possible approaches to this, but I would do one of the following:

有一些可能的方法,但我会做以下之一:

You can use mod_rewrite, as suggested by @LazyOne, to redirect only files with a .php extension to your custom document:

您可以使用@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]

Or you can direct everything through the PHP error handler as you already do, and use PHP to determine whether the requested resource is a PHP script or not:

或者您可以像通常那样通过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

The disadvantage of this second approach is that you may need to rewrite the standard error document so that it used PHP to get dynamic information to display to the user, such and the requested resource path.

第二种方法的缺点是您可能需要重写标准错误文档,以便它使用PHP来获取动态信息以显示给用户,例如所请求的资源路径。

#3


1  

Utilizing the $_SERVER['REQUEST_URI'], you can easily check the ending of the file that resulted in an error and then throw a header to a normal 404 if it doesn't correspond to your needs or just render a different 404

使用$ _SERVER ['REQUEST_URI'],您可以轻松检查导致错误的文件的结尾,然后如果它不符合您的需要或仅渲染不同的404,则将标头抛出到正常404

Put this in your 404.php file

把它放在你的404.php文件中

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

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