【问题标题】:htaccess Resource interpreted as Stylesheet but transferred with MIME type text/htmlhtaccess 资源被解释为样式表,但使用 MIME 类型 text/html 传输
【发布时间】:2018-01-23 04:27:08
【问题描述】:

我试图将我现有的 url 从 http://localhost/website/article.php?id=5 重写为 http://localhost/website/article/5 因此我的 htaccess 代码

RewriteBase /website/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule article/([a-zA-Z0-9@.-])? article.php?id=$1 [L]

我得到的是 Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost/website/article/css/bootstrap.min.css".

article 似乎被添加到文件的 url,甚至重定向文件目录。

真正的网址应该是"http://localhost/website/css/bootstrap.min.css"

我对 htaccess 很陌生,并试图通过从现有的复制来创建自己的

修订版

我的实际问题是我的资源 url 也被重命名。我已将代码行减少到 2 行

RewriteEngine On
RewriteRule article/([0-9@.-])  article.php?id=$1 

但现在资源被重定向到不存在的http://localhost/website/article/js/main.js 而不是http://localhost/website/js/main.js

【问题讨论】:

  • 也许您需要将/css/bootstrap.min.css 放入您的标记link 元素中?
  • 你知道吗,条件只用于第一次重写?文章重写规则总是被执行

标签: php apache .htaccess redirect


【解决方案1】:

添加重写条件以排除静态文件被重写:

RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]

【讨论】:

    【解决方案2】:

    我更喜欢用以下几行开始重写规则:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule  - [L] 
    

    它将排除所有现有文件 (-f) 和目录 (-d) 被重写。包括 .php 文件和 css 文件

    【讨论】:

      【解决方案3】:

      检查主 HTML 文件中的 URL;我怀疑浏览器在到达重写规则之前请求了错误的 URL。如果你有像<script src="js/main.js"> 这样的相对URL,那么在非重写版本中,浏览器会将其解析为http://localhost/website/js/main.js,但在重写版本中,它将解析为http://localhost/website/article/js/main.js,因为从浏览器的角度来看,HTML 文件位于目录http://localhost/website/article/,因此它相对于该目录进行解析。如果这是问题,您可以将其更改为 src="/website/js/main.js"src="../js/main.js"

      【讨论】:

        猜你喜欢
        • 2017-02-23
        • 1970-01-01
        • 2012-05-20
        • 2014-11-19
        • 2013-05-04
        • 2011-12-13
        • 2017-10-12
        • 2016-03-19
        • 2021-04-25
        相关资源
        最近更新 更多