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 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state