DOCTYPE 导致外部样式表在使用 PHP 作为处理程序时不起作用


DOCTYPE causing external stylesheet to not function when using PHP as handler

当通过 Apache2 的 SetHandler(一个 PHP 脚本)传递请求以及在文档中指定 DOCTYPE 时,我在外部样式表工作时遇到问题。 如果没有声明 DOCTYPE,页面可以很好地与外部样式表一起使用。 这是在Apache 2.2.15,PHP 5.3.3以及Apache 2.4.6,PHP 5.5.3下。

我的 Apache 配置:

<VirtualHost *:80>
DocumentRoot /data/www/test
<Directory "/data/www/test/_auth">
    Require all granted
</Directory>
Action VerifyAuth /_auth/authenticate_test.php
<Directory "/data/www/test/testsite">
    SetHandler VerifyAuth
    Require all granted
</Directory>
</VirtualHost>

我的authenticate_test.php文件:

<?php include($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']); ?>

我正在使用的索引.html文件:

<!DOCTYPE HTML>
<html>
<head>
<link href="/testsite/mainstyle.css" type="text/css" rel="stylesheet" media="all">
</head>
<body>
 <h1>Test Site</h1>
</body></html>

我正在测试的样式表(mainstyle.css):

h1 { background:black; color:blue; width:100px; display: block;}

当我检查日志时,我可以看到样式表正在以 200 返回代码读取,但样式表格式未应用。 同样,当删除 DOCTYPE 时,页面会按预期加载。 我可以将样式嵌入到 html 文件中,该文件也可以工作,但我需要能够使用外部样式表。

我试图完成的是让所有请求通过用 PHP 编写的身份验证脚本传递,用于在 testsite 目录下访问的任何和所有内容。 因为有些内容我不想通过从每个页面中删除所有 DOCTYPE 声明来自定义,所以无论是否声明了 DOCTYPE,我都希望它正常工作。

我已经在Chromium 28.0.1500.71(ubuntu 13.04),Firefox 24.0(ubuntu 13.04)和IE9(Win 7)上尝试过这个。

不幸的是,我刚才才在堆栈溢出上发现这个(页面头部的 DOCTYPE 导致session_problem)。

将我的身份验证 PHP 处理程序更改为此处理程序已解决我的问题:

<?php
$type =
(get_object_vars(apache_lookup_uri($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'])));
header('Content-type: '.$type['content_type']);
include($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']);
?>