【问题标题】:How to solve CSS include error after Apache RewriteRule?如何解决 Apache RewriteRule 后的 CSS 包含错误?
【发布时间】:2012-01-17 10:32:49
【问题描述】:

我使用 winXP 和 AppServ。我的网站根目录上有一个“showitem.php”。一个示例用法: www.mydomain.com/showitem.php?id=123
我想使用如下链接:
www.mydomain.com/item/123
在 .htaccess 中我写下这一行:
RewriteRule ^item/([^/]+)$ showitem.php?id=$1

服务器指向showitem.php 并且成功接收到id。但是,主要问题在于 css 和 js 文件。如果我将“css/style.css”设置为“/css/style.css”,页面会显示在 Internet 上,但不会显示在 localhost 上,因为 root 是“localhost”,但文件位于“localhost/mydomain”下。我也尝试过 .css 文件的“!-f”条件,但它没有帮助,据我所知,它不能。为了解决这个问题,我应该将错误的 css 文件解释指向正确的位置,因此我想将客户端请求“item/css/style.css”重定向到其原始位置“css/style.css”。我为localhost添加了以下规则,但还有其他方法吗?

RewriteRule ^item/css/([^/]+)$ /mydomain/css/$1
RewriteRule ^item/img/([^/]+)$ /mydomain/img/$1

【问题讨论】:

    标签: css apache rewrite localhost


    【解决方案1】:

    在你的 html 头部设置一个基本标签。

    <base href="http://www.mydomain.com/" />

    然后设置您的样式表链接路径相对于基础,例如

    <link href="css/style.css" />

    【讨论】:

    • 由于我同时开发 css、js 和 php,而且它们在我的本地主机上,这对我来说并不实用。
    • 为什么这不切实际?将其设置为http://localhost/。我刚刚从您的示例中获取数据。
    【解决方案2】:

    您可以将这些写入文件.htaccess

    ErrorDocument 404 /error_404.php
    

    /error_404.php 是您要在其中放一些东西的错误文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2013-01-30
      • 1970-01-01
      • 2021-12-16
      相关资源
      最近更新 更多