使用 htaccess 时未应用样式
我想根据所选国家/地区显示网址,因此我使用 htaccess 根据所选国家/地区重定向网址。例如,我希望网址如下所示:www.example.com/us/index.html
我的文件如下所示,
public_html/includes/style.css
public_html/index.php
在index.php中我链接了样式表,例如 当我尝试 www.example.com
时,“../includes/style.css
”
正确应用了样式。一旦用户在index.php中选择了国家/地区,URL将被重定向为www.example.com/us/index.html
。但对于这个重定向的页面,样式没有正确应用。
我在 .htaccess 中有以下条件来重定向页面,
RewriteRule ^([^/]+)/([^\.]+)\.html$ index.php?cnt=$1 [L]
谢谢。
I want to show the url based on country selected, so i have used htaccess to redirect url based on the country selected. for example, i want the url to be like this: www.example.com/us/index.html
i have my files like the following,
public_html/includes/style.css
public_html/index.php
in index.php i've linked the style sheet like "../includes/style.css
"
when i tried www.example.com
the style applied correctly. once the user selected the country in index.php, url will be redirected as www.example.com/us/index.html
. But for this redirected page the style was not applied correctly.
i've following condition in .htaccess to redirect the page,
RewriteRule ^([^/]+)/([^\.]+)\.html$ index.php?cnt=$1 [L]
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当访问 www.example.com/us/index.html 时,style.css 计算出的 url 是 www.example.com/us/includes/style.css ,这显然不存在。
因此,根据您的意图,要么
When visiting www.example.com/us/index.html, the style.css computed url is www.example.com/us/includes/style.css which obviously doesn't exists.
So, depending of what your intend is, either
如果您使用相对路径包含 CSS 文件,则当您的 URL 移出初始主目录(或使用 .htaccess 时似乎移出)时,CSS 将返回 404。
解决方案是在包含时使用绝对路径你的CSS
而不是
../includes/style.css
使用
/includes/style.css
$webroot
这里是项目 webroot 的完整路径If you're including your CSS files with relative paths your CSS will return a 404 when your URL moves beyond the initial home directory (or seems to move out, when using .htaccess)
The solution to this is to use an absolute path when including your css
instead of
../includes/style.css
Use
<?php echo $webroot; ?>/includes/style.css
$webroot
here being the full path to the webroot of your project它应该是
/includes/style.css
,而不是 ../includes/style.css,仅此而已。始终使用绝对路径。
it should be
/includes/style.css
, not ../includes/style.css, that's all.always use absolute path.
感谢您的帮助。这是我用来解决问题的代码:
但是,只有当我在本地主机中运行它时,它才对我有用,因为我将文件放在
home/localhost/example/
但是当我把它放在服务器上时,我再次遇到了样式问题。所以我只是像这样更改了 home 变量:
所以现在路径变成了,
而不是
Thanks for all your help. This is the code which i used to solve the issue:
However it worked for me only when i run this in localhost Because i put my files under
home/localhost/example/
But when i put it on server, again i faced the style problem. so i simply changed the home variable like this :
so now the path becomes,
instead of