与404页PHP一起使用的Echo最终目标URL

时间:2018-08-13 01:55:58

标签: javascript php

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

myurl.com/ thiswasmistyped.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/ thiswasmistyped.php ,它将显示为:

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

没有扩展名。我已经配置了.htaccess来做到这一点。我只需要根据页面URL更新内容。这可能吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

  

需要根据页面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)) ;