【问题标题】:Clean-URL -> URL's with only 1 parameter works, but with more than 1 parameter CSS-file will not be includedClean-URL -> 只有 1 个参数的 URL 有效,但不包含超过 1 个参数的 CSS 文件
【发布时间】:2014-08-17 20:30:28
【问题描述】:

我在我的项目中使用 Clean-URL。为简单起见,我简化了代码。

我的 .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

我的 index.php:

<?php
    
    require_once "/login.php";
        
?>

.css 文件包含在 login.php 的 HTML 部分中:

<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">

我的项目结构:

在根文件夹“test”中有 index.php、login.php 和 bootstrap 目录。

现在我的问题是:

如果我请求一个只有 1 个参数的 URL,例如

http://localhost/test/bla then all working!

但是如果我请求一个带有 2 个参数的 URL,例如:

http://localhost/test/bla/bla 

然后显示登录页面,但没有所有 css 样式(不包括 css 文件...)。

但是为什么呢?

【问题讨论】:

    标签: .htaccess mod-rewrite url-routing clean-urls


    【解决方案1】:

    首先,如果您的 htaccess 在 test 文件夹中,它应该是这样的

    RewriteEngine On
    RewriteBase /test/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    

    然后,您还必须为您的 css 和 js 链接添加一个重写基础(用于具有许多虚拟目录的绝对路径)

    <link href="/test/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    

    但你可以简单地在你的 html 页面中使用它并保持你的 css/js 链接不变

    <base href="/test/" />
    

    【讨论】:

    • 坦克很多! (不需要基本标签)。
    • 不客气。就像我说的没有必要,它只是避免你在每个 css/js 链接中重复/test/
    【解决方案2】:

    发生这种情况是因为您将 css 包含在相对 url 中,因此您实际上是在尝试加载

    http://server.com/test/bla/bootstrap/css/bootstrap.min.css
    

    而不是

    http://server.com/test/bootstrap/css/bootstrap.min.css
    

    这不是一个干净的 URL 问题。快速处理方法:

    <link href="/test/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    

    毕竟,对于给定的项目,资产目录不容易经常更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-11
      • 2016-05-19
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多