首页 > 解决方案 > 在php中链接资源

问题描述

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

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

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

所有 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.csscss/main.css但是每当遇到错误时,这都会导致 404.html 中的 CSS 混乱。我尝试的另一个尝试是/css/main.css,但再次没有运气服务器正在寻找http://localhost/css/main.css

那么,如何链接这些文件?是否有任何可用的配置.htacccess可以说 server to look /(root) as /App,所以如果有任何配置,那么我可以通过说/css/main.css,/html/another.html等等来让一切正常工作?或者有什么其他建议?

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

标签: php.htaccess

解决方案


将以下代码放入您的 .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


推荐阅读