简体   繁体   English

将 CSS 文件链接到 CGI 程序中的 HTML

[英]Link the CSS file to the HTML one in CGI programm

So, I recently started to code a web interface with CGI, after finding how to print hello world on my Apache web page with HTML, I wanted to add some CSS to it but apparently it doesn't work.所以,我最近开始用 CGI 编写一个 web 接口,在找到如何在我的 Apache web 页面上使用 HTML 打印 hello world 之后,我想向它添加一些 CSS 但显然它不起作用。

I'm on Linux, and I am working with Apache2.我在 Linux,我正在使用 Apache2。

So there is my source file hello.c (which is in /usr/lib/cgi-bin):所以有我的源文件 hello.c(在 /usr/lib/cgi-bin 中):

#include <stdio.h>

int main()
{
    printf(
        "Context-type:text/html\n\n"
        "<html>\n"
        "<head>\n"
        "<title> Hi world </title>\n"
        "<link type=\"text/css\" rel=\"stylesheet\" href=\"style/style.css\">\n"
        "</head>\n"
        "<body>\n"
        "<h2>Hello world !</h2>\n"
        "</body>\n"
        "</html>\n"
    );

    return 0;
}

and there is my CSS file (which is in /usr/lib/cgi-bin/style/):还有我的 CSS 文件(在 /usr/lib/cgi-bin/style/ 中):

body {
    background-color:rgb(255, 0, 170); 
}

h2 {
    font-family: Times;
    font-size: 38pt;
}

I already tried to put my CSS file in my home directory but it doesn't work too.我已经尝试将我的 CSS 文件放在我的主目录中,但它也不起作用。

If you check your.network panel of your browser's debugger , you're probably encountering a 500 status, as the server refuses to serve /cgi-bin/style/style.css .如果您检查浏览器调试器的your.network 面板,您可能会遇到500状态,因为服务器拒绝提供/cgi-bin/style/style.css

Generally, style sheets do not belong in the CGI directory.通常,样式表不属于 CGI 目录。 You should move them to somewhere in your DocumentRoot path.您应该将它们移动到DocumentRoot路径中的某个位置。


Assuming your configuration is something like:假设您的配置类似于:

DocumentRoot "/var/www"
ScriptAlias /cgi-bin/ "/usr/lib/cgi-bin/"

Set your directory structure to:将您的目录结构设置为:

/usr/lib/
└── cgi-bin
    └── hello.cgi
/var/www
└── style
    └── style.css

Then change the relative path然后更改相对路径

"<link type=\"text/css\" rel=\"stylesheet\" href=\"style/style.css\">\n"

to an absolute path (note the / prefix):到绝对路径(注意/前缀):

"<link type=\"text/css\" rel=\"stylesheet\" href=\"/style/style.css\">\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM