繁体   English   中英

如何在地址栏中保留输入错误的URL,同时仍向用户显示未找到的页面?

[英]How can I keep a mistyped URL in the address bar while still showing a not found page to the users?

这是我要寻找的示例: https : //www.sitepoint.com/dhbfh/

如果您访问上述链接,则不会重定向到未找到的页面。 地址栏中的原始URL保持不变,但页面内容仍显示未找到消息。 我怎样才能做到这一点?

当前,我在.htaccess使用以下行,而没有其他操作。

ErrorDocument 404 https://mywebsite.com/not-found.php

我是否需要删除此行,或者是否需要执行其他操作来实现未找到的位置(如SitePoint示例)?

示例url使用.htaccess文件中的url重写。 您可以将与现有文件不对应的所有网址发送到错误文档:

RewriteEngine On

#if the request is an existing file, show the file
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

#else show your 404 page
RewriteRule ^(.+)$ /not-found.php [QSA,L]

另外,请确保not-found.php页面发送正确的404标头:

header("HTTP/1.1 404 Not Found");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM