【问题标题】:Linking resources in php在php中链接资源
【发布时间】:2021-09-07 20:28:13
【问题描述】:

我的应用程序具有以下文件结构

/App
    /html
        404.html
        another.html
        other.html
    /css
        main.css
    .htaccess
    index.php

我正在从index.htmlreadfile() 发送other.html 文件。

所有 html 文件共享main.css

index.html 看起来像这样

<html>
   <head>
      <link rel="stylesheet" href="../css/main.css">
   </head>
   <body>
      <a href="another.html">link</a>
   </body>
</html>

我在 XAMPP 上运行它。与

    http://localhost/App

我能够检索 index.html,但是当我点击链接时,我得到了 404.html,因为我写了 another.html../css/main.css,服务器正在寻找 http://localhost/App/another.htmlhttp://localhost/css/main.css .这会导致 404.html 和 index.html 中的 CSS 混乱

我尝试通过将../css/main.css 替换为css/main.css 来解决此问题,但这会在遇到错误时导致404.html 中的CSS 混乱。我尝试的另一个尝试是/css/main.css,但同样没有运气服务器正在寻找http://localhost/css/main.css

那么,我如何链接这些文件? .htacccess 中是否有任何可用配置可以说服务器将/(根)视为/App,所以如果有任何配置,那么我可以通过说/css/main.css/html/another.html 和很快?还是有其他建议?

这是问题的视频:https://youtu.be/Qg7K8gOdyW0

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    在你的 .htaccess 文件中放入以下代码:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/App/
    RewriteRule ^(.*)$ /App/$1 [L,R=301]
    

    在 index.html 中使用下一个链接:

    /html/another.html
    /css/main.css
    

    看看这个: https://www.youtube.com/watch?v=oO1AHIOec3c

    【讨论】:

    • 哦!我忘了提到我在某人的问题中找到了这段代码,但它不起作用:(
    • 好的,我让它工作了,但我必须为不同的文件定义不同的路径。似乎我不必更改.htaccess。我所做的更改:在another.html 中,我将路径更改为相对路径../css/main.css,而对于other.html,路径是css/main.css。 404.html 仍然没有为所有路径解析
    • 应该没问题,看看我为你附上的视频。
    • 你看过视频了吗?
    • 您是否找到了仍然相关的解决方案或问题?实际上,您不需要 readfile(),将默认页面放入 index.php,您尝试使用 readfile 加载的页面
    猜你喜欢
    • 2010-09-06
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多