Css包含在PHP中实现modrewrite时不工作

时间:2014-07-20 23:41:41

标签: php html css

我正在研究wamp。 我网站的链接是:localhost / tshirtshop /

在重写之前是:localhost / tshirtshop / index.php?product = 12

重写后是:localhost / product-title-p12

问题: 一切都很好。但问题在于Css文件。

这个文件不包括..当我看到源代码(Ctrl + U)时,路径是这样的:

本地主机/ tshirtshop /产品所有权-P12 /风格/ tshirtshop.css

我尝试了什么:

我试过这个

<style>
    <?php include 'styles/tshirtshop.css';
   </style>

它起作用但不适合我。因为还有一些其他图像也包括在内,即标识 结果如下:    localhost / tshirtshop / product-title / images / tshirtshop.png

这可能是一个非常简单的解决方案。但我一小时后面临问题。我试图谷歌搜索类似的问题,但没有找到任何。所以请不要介意。抱歉我的英语。

4 个答案:

答案 0 :(得分:1)

CSS不像典型的服务器脚本那样包含在PHP中,它通过客户端Web浏览器呈现的HTML进行链接。

所以不要这样:

<style>
    <?php include 'styles/tshirtshop.css';
</style>

试试这个:

<link rel="stylesheet" type="text/css" href="/styles/tshirtshop.css">

答案 1 :(得分:1)

使用像

这样的绝对路径
<link rel="stylesheet" type="text/css" href="http://localhost/tshirtshop/styles/tshirtshop.css" />

答案 2 :(得分:0)

是的,你应该避免使用css和js的相对路径。如果您使用Apache的mod_rewrite使用seo url,那么只有完整路径才能为您提供帮助。所以这个:

<link rel="stylesheet" type="text/css" href="theme.css">

页面

http://www.mypage.com/seo-url

在以下位置搜索css文件:

http://www.mypage.com/seo-url/theme.css

不存在。虽然这个:

http://www.mypage.com/theme.css

是你想要的。所以从一开始就使用:

<link rel="stylesheet" type="text/css" href="http://www.mypage.com/theme.css">

你完成了。与js相同。

答案 3 :(得分:0)

如果不查看您的.htaccess文件( 如果您可以包含我们可以帮助您进一步调试 ),我无法确切地说出问题是什么。 但我猜想它只有一个RewriteRule

在重写规则之前你应该包括的内容是:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Your rewrite rule here...

还有其他选择。您可以在HTML标记中指定<base>- Read More

看起来像这样:

<base href="http://www.example.com/">

这将允许您使用您当前正在进行的相对路径。你的其他选择是按照其他答案所说的那样做,但是谁想要查看所有代码并重新写下所有这些内容?