dongmo9996 2013-06-28 23:24
浏览 34

因为htaccess规则包括带有css和javascript中断的标头

I have the following structure on my php project:

- classes
    - model.php
- views
    - header.php
    - footer.php
    - user.php
- resources
    - css
        - stylesheet.css
    - js
        - my_javascript.js
- index.php
- controller.php

.htaccess file

RewriteEngine On
RewriteRule ^/?resources/(.*)$ resources/$1 [L]
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z0-9/]*)$ index.php?page=$1&query=$2 [L]

header.php

<!DOCTYPE html>
<html>
    <head>
       <!-- Styles -->
       <link href="resources/css/stylesheet.css" rel="stylesheet" media="screen">
    </head>
    <body>

The problem, as many others have mentioned on stackoverflow before (but whose solutions have not worked for me), is that Im currently redirecting all pages through index.php and therefore even my resources files, such as stylesheet.css.

1: When I ask for mydomain/user I get the correct resources loaded with right css (mydomain/resources/css/stylesheet.css)

2: But as Im trying to make the app RESTful, I have a page such as mydomain/user/4 and when I make a request such as this my css suddently doesn´t load. Looking at the http request its looking for (mydomain/user/resources/css/stylesheet.css)

I have tried to solve it by including $_SERVER['DOCUMENT_ROOT']. It does not work, and does not seem to be a "nice" solution. Thanks for your help. And yes I am a newbie at php!

  • 写回答

2条回答 默认 最新

  • dongyongan9941 2013-06-29 05:38
    关注

    Since you are masking the URL and the actual scripts runs on a different folder structure than what is found in the URL, you have to provide full path to the CSS, images, Javascripts etc you refer in your program.

    You can change your code to following

    <link href="http://www.yourdomainname.com/resources/css/stylesheet.css" rel="stylesheet" media="screen">
    

    Or use relative path to the files

    <link href="/resources/css/stylesheet.css" rel="stylesheet" media="screen">
    

    Since we are using / at the starting of file path, server will look for the file from the root working directory of the website.

    Along with this you should add conditions in your htaccess file like below

    RewriteCond %{REQUEST_FILENAME} !(resources|img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
    RewriteRule ^([a-zA-Z]+)/?([a-zA-Z0-9/]*)$ index.php?page=$1&query=$2 [L]
    

    I hope you will not need RewriteRule ^/?resources/(.*)$ resources/$1 [L] in the htaccess after making these alteration

    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题