资源解释为样式表,但以MIME类型text/html传输,并在PHP中生成css


Resource interpreted as Stylesheet but transferred with MIME type text/html with css generated in PHP

我有一个网站的CSS不解释。我见过其他答案,但没有一个是动态生成的css。我在PHP中通过添加

来生成CSS
<link media="all" type="text/css" href="http://tylldalil.wconsult.no/?ai1ec_render_css=1367403986&ver=3.5.1" id="ai1ec_stytle-css" rel="stylesheet">

my function in php

public function render_css() {
     header( 'Content-Type: text/css' );
    // Aggressive caching to save future requests from the same client.
    $etag = '"' . md5( __FILE__ . $_GET[self::GET_VARIBALE_NAME] ) . '"';
    header( 'ETag: ' . $etag );
    $max_age = 31536000;
    header(
        'Expires: ' .
        gmdate(
            'D, d M Y H:i:s',
            Ai1ec_Time_Utility::current_time() + $max_age
        ) .
        ' GMT'
    );
    header( 'Cache-Control: public, max-age=' . $max_age );
    if (
        empty( $_SERVER['HTTP_IF_NONE_MATCH'] ) ||
        $etag !== stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] )
    ) {
        // compress data if possible
        if ( extension_loaded( 'zlib' ) ) {
            // ob_start( 'ob_gzhandler' );
        }
        $content = $this->get_compiled_css();
        echo $content;
        ob_end_flush();
    } else {
        // Not modified!
        status_header( 304 );
    }
    // We're done!
    exit( 0 );
}

这是我的html

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?tlldalil.wconsult.no$
RewriteRule ^(/)?$ ?page_id=23 [R=301,L]
order deny,allow
deny from all
allow from 88.89.104.103 
allow from 93.50.99.14

###Start Kloxo PHP config Area
###Please Don't edit these comments or the content in between. kloxo uses this to recognize the lines it writes to the the file. If the above line is corrupted, it may fail to recognize them, leading to multiple lines.
<Ifmodule mod_php4.c>
    php_value error_log "/home/klausen/__processed_stats/tylldalil.wconsult.no.phplog"
    php_value upload_max_filesize 64M
    php_value max_execution_time  60
    php_value max_input_time  120
    php_value memory_limit  64M
    php_value post_max_size  64M
    php_flag register_globals  off
    php_flag display_errors  off
    php_flag file_uploads  on
    php_flag log_errors  off
    php_flag output_buffering  off
    php_flag register_argc_argv  on
    php_flag magic_quotes_gpc   off
    php_flag magic_quotes_runtime  off
    php_flag magic_quotes_sybase  off
    php_flag mysql.allow_persistent  off
    php_flag register_long_arrays  on
    php_flag allow_url_fopen  on
    php_flag cgi.force_redirect  on
    php_flag enable_dl  on
</Ifmodule>
<Ifmodule mod_php5.c>
    php_value error_log "/home/klausen/__processed_stats/tylldalil.wconsult.no.phplog"
    php_value upload_max_filesize 64M
    php_value max_execution_time  60
    php_value max_input_time  120
    php_value memory_limit  64M
    php_value post_max_size  64M
    php_flag register_globals  off
    php_flag display_errors  off
    php_flag file_uploads  on
    php_flag log_errors  off
    php_flag output_buffering  off
    php_flag register_argc_argv  on
    php_flag magic_quotes_gpc   off
    php_flag magic_quotes_runtime  off
    php_flag magic_quotes_sybase  off
    php_flag mysql.allow_persistent  off
    php_flag register_long_arrays  on
    php_flag allow_url_fopen  on
    php_flag cgi.force_redirect  on
    php_flag enable_dl  on
</Ifmodule>
###End Kloxo PHP config Area


# BEGIN WordPress
# END WordPress

我应该添加什么到我的htaccess?

当我试图查看你的CSS,我得到一个"Apache 2测试页面由CentOS" HTML页面代替。页面响应头声明"403 Forbidden"。

这可能与你的主动缓存有关吗?

如果在PHP中修改了

行,会发生什么?
$etag = '"' . md5( __FILE__ . $_GET[self::GET_VARIBALE_NAME] ) . '"';

$etag = '"' . md5( __FILE__ . $_GET[self::GET_VARIABLE_NAME] ) . '"';

?

您在php中设置了Content-type,但似乎服务器强制该脚本使用另一种Content-type。你可以尝试为你的脚本设置css扩展名,然后用mod_rewrite:

重写它
<link media="all" type="text/css" href="http://tylldalil.wconsult.no/css_1367403986_ver_3.5.1.css" id="ai1ec_stytle-css" rel="stylesheet">
在. htaccess:

RewriteEngine On
RewriteCond %{ENV:REDIRECT_FINISH} !^$
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME}  !-f
RewriteRule css_([0-9])+_ver_([0-9.]+)'.css$ ?ai1ec_render_css=$1&ver=$2 [E=FINISH:1,L]
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(/)?$ ?page_id=23 [R=301,L]