如何在地址栏中保留输入错误的 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/
如果您访问上述 link,您不会被重定向到未找到的页面。地址栏中原来的URL保持不变,但页面内容仍然显示未找到消息。我该怎么做?
目前,我在 .htaccess
中使用以下行,仅此而已。
ErrorDocument 404 https://mywebsite.com/not-found.php
我是否需要删除此行,或者我是否需要做其他事情来实现像 SitePoint 示例一样的未找到?
示例 url 在 .htaccess
文件中使用 url-rewriting。您可以将所有与现有文件不对应的 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:
header("HTTP/1.1 404 Not Found");
这是我正在寻找的示例:https://www.sitepoint.com/dhbfh/
如果您访问上述 link,您不会被重定向到未找到的页面。地址栏中原来的URL保持不变,但页面内容仍然显示未找到消息。我该怎么做?
目前,我在 .htaccess
中使用以下行,仅此而已。
ErrorDocument 404 https://mywebsite.com/not-found.php
我是否需要删除此行,或者我是否需要做其他事情来实现像 SitePoint 示例一样的未找到?
示例 url 在 .htaccess
文件中使用 url-rewriting。您可以将所有与现有文件不对应的 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:
header("HTTP/1.1 404 Not Found");