使用 htaccess 时未应用样式

发布于 2024-10-22 09:33:17 字数 568 浏览 3 评论 0原文



我想根据所选国家/地区显示网址,因此我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过度放纵 2024-10-29 09:33:17

当访问 www.example.com/us/index.html 时,style.css 计算出的 url 是 www.example.com/us/includes/style.css ,这显然不存在。

因此,根据您的意图,要么

  • 使用样式表的绝对链接,例如“/includes/style.css”,
  • 创建一个 htaccess 重写规则来修复 url,
  • 为每个国家/地区创建 1 个样式表

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

  • use a absolute link for the style sheet like "/includes/style.css"
  • create a htaccess rewrite rule to fix the url
  • create 1 style sheet per country
关于从前 2024-10-29 09:33:17

如果您使用相对路径包含 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

深海夜未眠 2024-10-29 09:33:17

它应该是 /includes/style.css,而不是 ../includes/style.css,仅此而已。
始终使用绝对路径。

it should be /includes/style.css, not ../includes/style.css, that's all.
always use absolute path.

怀中猫帐中妖 2024-10-29 09:33:17

感谢您的帮助。这是我用来解决问题的代码:

$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'/>

但是,只有当我在本地主机中运行它时,它才对我有用,因为我将文件放在 home/localhost/example/
但是当我把它放在服务器上时,我再次遇到了样式问题。所以我只是像这样更改了 home 变量:

$home="";

所以现在路径变成了,

/includes/style.css 

而不是

//includes/style.css

Thanks for all your help. This is the code which i used to solve the issue:

$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'/>

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 :

$home="";

so now the path becomes,

/includes/style.css 

instead of

//includes/style.css
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文