回显用于 404 页 PHP 的最终目标 URL



我正在尝试创建一个 404 页面,该页面仅将 URL 的末尾显示为段落的一部分,例如;

myurl.com/这是打错了.php

我不希望用户能够看到.php扩展名,也看不到除页面之外的 URL 路径的其余部分。

这是我到目前为止所拥有的。

<?php
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$page = basename($_SERVER['PHP_SELF']); // Get script filename without any path information
$url = str_replace( array( '.php', '.htm', '.html' ), '', $page ); 
$page ); //
$page = str_replace( array('-', '_'), ' ', $page); 
$page = ucwords( $page ); //
$url = ucwords( $url );
?>
<p>We don't think you meant to come to <?php echo $url; ?>. Please try finding another page :)</p>

理想情况下,错误页面名称将是大写的,因此最终结果将是这样的;

We don't think you meant to come to <?php echo $url; ?> Please try finding another page :)

但基于myurl.com/这是错误输入.php,它将显示为:

We don't think you meant to come to Thiswasmistyped Please try finding another page :)

没有扩展名。我已经配置了.htaccess来执行此操作。我只需要根据页面 URL 更新内容。这可能吗?

提前谢谢。

需要根据页面URL更新内容

如果我真的理解您的问题并根据您的代码,您可以使用 $_SERVER 变量动态更改您的内容。

您可以使用请求 URI:

ucfirst(substr($x=substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'.')),strrpos($x,'/')+1)) ;

或脚本文件名:

ucfirst(substr($x=substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['REQUEST_URI'],'.')),strrpos($x,'/')+1)) ;

甚至像在您自己的代码中一样,PHP_SELF索引也指向相同的脚本:

ucfirst(substr($x=substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'.')),strrpos($x,'/')+1)) ;

最新更新