【问题标题】:php rewriteEngine using .htaccess not showing css使用.htaccess的php rewriteEngine不显示css
【发布时间】:2013-07-29 22:24:52
【问题描述】:

我使用 .htaccess 来重写 url。请参阅下面的示例:

我的文件夹树形是这样的:

  • 我的网站(文件夹)
    • css(文件夹)
      • style.css
    • mypage.php
    • .htaccess

当我调用http://localhost/mysite/mypage/myid 时,它被重定向到http://localhost/mysite/mypage.php?id=myid

那么问题是它似乎没有调用我的 css 是:

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

我的 .htaccess 的内容是:

RewriteEngine on
RewriteBase /mysite
RewriteRule ^mypage/([^/]*) mypage.php?id=$1

提前致谢

编辑:

我尝试了解决方案:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

我仍然无法调用 css :/

编辑 2:

好的,我可以像这样调用我的 css 来让它工作:

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

看起来我还必须使用路径“/mysite/”调用我在 html 中的所有图像

【问题讨论】:

    标签: php css .htaccess mod-rewrite


    【解决方案1】:

    你的样式现在被称为相对,所以你应该称之为绝对。

    没有 /myid/css/ 文件夹,这就是您的代码现在正在寻找的。​​p>

    <link href="<?php echo $_SERVER['SCRIPT_URI'];?>css/style.css" rel="stylesheet" type="text/css" />
    

    【讨论】:

    • 该函数的结果是否应该写成 localhost/mysite/css/style.css"> ?因为我使用该函数得到了 php 错误(我在 windows Wamp srv 上)。谢谢
    • 是的,在实时服务器上,它会回显当前页面的完整 uri。但是因为您在本地主机上运行,​​所以它无法以某种方式工作。它应该说 (http://)localhost/mysite/css/style.css。
    • 好的,所以我还必须为所有 添加此功能对吗?没有其他方法吗?
    【解决方案2】:

    你需要告诉重写引擎忽略实际存在的目录和文件:

    RewriteEngine on
    RewriteBase /mysite
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^mypage/([^/]*) mypage.php?id=$1
    

    【讨论】:

      【解决方案3】:

      添加重写条件:

      RewriteCond %{REQUEST_FILENAME} !-d [OR]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^mypage/([^/]*) mypage.php?id=$1
      

      现在您的 css(以及其他文件和目录)将不会通过 rewriterule 发送

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 2014-08-25
        • 1970-01-01
        • 1970-01-01
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        相关资源
        最近更新 更多