Php 使用htaccess时未应用样式
Php 使用htaccess时未应用样式,php,css,.htaccess,redirect,Php,Css,.htaccess,Redirect,我想根据所选国家显示url,因此我使用htaccess根据所选国家重定向url。例如,我希望url如下:www.example.com/us/index.html 我的档案如下: public_html/includes/style.css public_html/index.php 在index.php中,我链接了样式表,如“。/includes/style.css” 当我尝试www.example.com时,样式应用正确。一旦用户在index.php中选择了国家,url将被重定向为
我想根据所选国家显示url,因此我使用htaccess根据所选国家重定向url。例如,我希望url如下:
www.example.com/us/index.html
我的档案如下:
public_html/includes/style.css
public_html/index.php
在index.php中,我链接了样式表,如“。/includes/style.css
”
当我尝试www.example.com
时,样式应用正确。一旦用户在index.php中选择了国家,url将被重定向为www.example.com/us/index.html
。但对于此重定向页面,样式未正确应用
我在.htaccess中设置了以下条件以重定向页面
RewriteRule ^([^/]+)/([^\.]+)\.html$ index.php?cnt=$1 [L]
谢谢。如果您将CSS文件包含在相对路径中,当您的URL移动到初始主目录之外时(或者使用.htaccess时,似乎要移出),CSS将返回404 解决方法是在包含css时使用绝对路径 而不是
。/包括/style.css
使用/includes/style.css
$webroot
这里是项目的webroot的完整路径当访问www.example.com/us/index.html时,style.css计算url是www.example.com/us/includes/style.css,它显然不存在
所以,这取决于你的意图
- 对样式表使用绝对链接,如“/includes/style.css”
- 创建htaccess重写规则以修复url
- 每个国家/地区创建一个样式表
/includes/style.css
,而不是../includes/style.css,仅此而已。始终使用绝对路径
谢谢你的帮助。这是我用来解决问题的代码:
$base = $_SERVER['REQUEST_URI'];
$base=parse_url($base);
$parts=explode("/",$base['path']);
$path=$parts[1];
$home="/".$path;
<link rel='stylesheet' type='text/css' href='$home/includes/style.css'/>
所以现在这条路变成了
/includes/style.css
而不是
//includes/style.css
谢谢我也使用了同样的方法,在标题中我有一个变量$home,它保存着类似$home=“../”的路径。链接样式表时,我使用了href=“”。对吗?不对$家仍然是一条相对的道路。您需要获取域的基本URL。如果您使用的是虚拟主机,您可以简单地使用/includes/style.css,但是如果您使用的是类似的东西,那么您必须弄清楚index.php中的webroot是什么。你有什么样的设置?你是对的。出于测试目的,我将文件放在“localhost/projects/sample/”下。示例文件夹包含includes/style.css等目录和index.php.htaccess等文件。在php中是否有任何特定的函数来获取baseurl?好的,如果index.php文件在
/sample
中,并且所有请求都到达该文件,而css文件在/sample/includes/
中,那么您可以使用dirname(\uu file\uuu)
来获取当前目录,然后将/includes
附加到它。如果您使用的是框架,那么就容易多了。PHP链接:
//includes/style.css