douchen9569 2015-05-22 11:25
浏览 32
已采纳

重定向用户时的CSS问题取决于浏览器语言

I have a silly problem when trying to redirect a user to a different version of the same site depending on the browser's language.

Actually the user seems to be properly redirected to the language site version, the problem is that the site doesn't load the CSS and JS files propperly (I think it is due to its folder structure), and I'm not sure about how can I solve it.

To perform the redirection I have set a file called 'index.php' which is placed at the root folder, it has the following PHP code:

<?php
  header("Location: " . getLanguage());

  function getLanguage(){
    $availableLanguages = array('es', 'en');
    $defaultLanguage = 'en';
    $token = strtok($_SERVER['HTTP_ACCEPT_LANGUAGE'], ",;");
    while ($token !== false) {
      $language = substr($token, 0, 2);
      if (in_array($language, $availableLanguages)) {
        return $language;
      }
      $token = strtok(",;");
    }
    return $defaultLanguage;
  }
?>

As you can see there are only 2 languages (english and spanish).

The next step was to place 2 different folders with the respective 'index.html' file on them:

/es --> for the spanish index.html version /en --> for the english index.html version

So we can see the following example folder structure at the server:

  • http/img/picture.jpg
  • http/js/script.js
  • http/css/style.css
  • http/en/index.html (the english version of the site)
  • http/es/index.html (the spanish version of the site)
  • http/index.php

When the user enters to the site, the php file redirects him to the '/en' or '/es' folder deppending on the browser language... Then the user loads the 'index.html' file with the followihg HTML structre:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>my site in english</title>
    <link rel='icon' href='img/favicon.png'>
    <!--Styles-->
    <link href="css/styles.css" type="text/css" rel="stylesheet">
</head>

<body>
<p>this is an example</p>
<!--Styles-->
<script src="js/script.js"></script>
</body>
</html>

So as you could see, the 'index.html' defines the paths for css,img and js files that are at placed at the 'root/css','root/img' and 'root/js' folders, however the website doesn't load them... and I don't know why?

  • 写回答

2条回答 默认 最新

  • dqf42223 2015-05-22 11:29
    关注

    The index file is now inside /en or /es, therefore:

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

    should be:

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

    note the added ../ to go back a folder. Same goes for the js and other resources you have.

    I would recommend using absolute paths, that way you will not face similar problems:

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

    Note the / at the beginning of the URI.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题