以下是用户user2305090提供的一种变体,适用于我:
首先,在您的.htaccess文件中添加以下内容,其中/forwarding-site-nocase.php是我的自定义404页面URL。
ErrorDocument 404 /forwarding-site-nocase.php
然后,在您的自定义404页面的顶部,从第一行开始,添加以下内容。如果先有一个空白行,则会失败。
<?php
$mydir=getdir("/",$_SERVER['REQUEST_URI']);
if($mydir!=false)
{
$thedomain = 'https://' . $_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" />
接下来的页面也是如此。
请注意,我将“http://”更改为“https://”,并明确将错误文件更改为.php,因为原始建议版本会引发错误。
RewriteMap
只能在服务器配置或虚拟主机上下文中声明,不能在.htaccess中声明。否则,规则是正确的。 - LazyOneRewriteMap tolower int:tolower
放入 .htaccess 文件中,您将会得到 500 服务器错误。 - LazyOne