上面的不起作用,但下面的起作用。在www.chassis-plans.com/off-the-shelf-industrial-pc.html 玩它。更改 URL 中的任何大小写,代码会将其更改为适当的大小写并显示页面。如果您输入的 URL 不存在 (www.chassis-plans.com/x),则会显示自定义 404 页面。
首先,将以下内容添加到您的 .htaccess 文件中,其中 /forwarding-site-nocase.html 是我的自定义 404 页面 URL。
AddType application/x-httpd-php .html .htm
ErrorDocument 404 /forwarding-site-nocase.html
然后,在自定义 404 页面的最顶部,从第 1 行开始,添加以下内容。如果你先有一个空行,它就会失败。
<?php
$mydir=getdir("/",$_SERVER['REQUEST_URI']);
if($mydir!=false)
{
$thedomain = 'http://' . $_SERVER['SERVER_NAME'];
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: ' . $thedomain.$mydir );
}
function getdir($loc,$tfile)
{
$startloc=$_SERVER['DOCUMENT_ROOT'];
if (file_exists($startloc.$loc) && $handle = opendir($startloc.$loc))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && strncasecmp($loc.$file,$tfile,strlen($loc.$file))==0)
{
if(strncasecmp($loc.$file,$tfile,strlen($tfile))==0)
{
return $loc.$file;
}
else
{
return getdir($loc.$file."/",$tfile);
}
}
}
closedir($handle);
}
return false;
}
?>
现在您可以使用您的标准 HTML 来为您的自定义 404 页面执行此操作,例如:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Company information page - Page Not Found Error"/>
<meta name="search" content="Company information page - About Us. Manufacturer of rack mount rugged computer systems and rugged LCD displays for the military and industrial markets" />
页面的其余部分以此类推。
我在开发和测试时遇到了 IE 问题,可能是缓存,但现在似乎可以正常工作了。