我正在尝试创建一个 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 更新内容。这可能吗?
提前致谢。
最佳答案
need the content to update based on the page 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)) ;
关于javascript - Echo 最终目标 URL 用于 404 页面 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51814293/